Deploying cx_Oracle in Windows

2019-01-26 19:09发布

问题:

I'm trying to use cx_Oracle module in python to access a remote database and insert or delete rows in tables. BTW I have downloaded oracle instant client 11.1 and also odbc and sqlplus versions. I have set environment variables ORACLE_HOME and TNS_ADMIN to directory I have unzipped files there and add this this directory to the PATH variable. As I googled I think there should be some .ora files but I can't find them.

I can use sqlplus commands from command line, but my main problem is that I can't install and use cx_Oracle. I don't want to use any command line scripts; I just want to use cx_Oracle as an API.

Can anyone give me a complete explanation, I'm really stuck there. All the documents in this area are vague. Best Regards.

EDIT 1:

I tried the source package of cx_Oracle with python setup.py install and python setup.py build --compiler=mingw32 commands. I don't get the oracle error again, but I'm getting command 'gcc' failed error. I tried MinGW compiler and have edited environment variable PATH to contain MinGW install directory(e.g. C:\MinGW); I have also installed Microsoft Visual C++(versions 2005, 2008 and 2010); but I still get the error.

EDIT 2:

Since @jpmc26 mentioned about tnsnames.ora files, I decided to add that I have tried to make a tnsnames.ora and also a sqlplus.ora file and put them in directory C:\oracle\instant_client_11_1\network\admin; but as I googled I realized that if you use cx_Oracle.connect(username, password, cx_Oracle.makedsn(ip, port, sid)) command, you can skip .ora files. cx_Oracle.makedsn would make the structure itself.

回答1:

I tried a lot of ways to finally solve the problem. Since I think I should write a comprehensive answer, I will write the problems that I faced and their solutions respectively; hoping it can help others. I also changed the question's name to an appropriate one. Here's what I've been through during installation of cx_Oracle module in Windows 7 - 32 bit version(I think other versions of windows would have similar solutions, but I'm not sure):

  • I have tried to install cx_Oracle using easy_install and I got the error No oracle client installed. I have downloaded oracle basic instant client and oracle sdk instant client for windows from Oracle Official Website and installed it via following steps:

    Unzip oracle basic instant client into directory C:\oracle\instant_client_11_1\.

    Unzip oracle sdk instant client and copy folder sdk into the directory C:\oracle\instant_client_11_1\, consequently we have a directory as C:\oracle\instant_client_11_1\sdk\.

    Add C:\oracle\instant_client_11_1\ to the end of the PATH environment variable.

    Add a new variable named ORACLE_HOME and set C:\oracle\instant_client_11_1\ for the value.

    Restart computer.

  • I tried to install cx_Oracle using easy_install again and I got error command ‘gcc’ failed: no such file or directory and in some cases unable to find vcvarsall.bat; this was because I didn’t have a C++ compiler, so I followed these steps to solve it:

    Download Microsoft Visual C++ and install it.

    Download MinGW and install it; note that you must install GCC module.

    Add C:\MinGW\ and C:\MinGW\bin\(which contains gcc.exe) to the end of the PATH environment variable.

  • I tried to install cx_Oracle using easy_install again and I got error command ‘gcc’ failed with exit status 1; I tried this step to solve it:

    Open file C:\Python27\Lib\distutils\ cygwincompiler.py and remove all –mno-cygwin occurrences in this file; this is because recent versions of GCC has removed –mno-cygwin option and it shouldn’t be there.

  • I tried to install cx_Oracle using easy_install and it worked.

But during searching in the internet I found a couple of tricks that may be useful:

  • If you got error no module named win32api, it’s because you didn’t install win32 extensions of python on your computer you should download pywin32 module from Source Forge and install it.

  • If you didn’t succeed anyway, you can try an older version of cx_Oracle; but keep in mind all we talked about in the above lines. You can find all versions of cx_Oracle here.

  • If you want to use sqlplus you should do some other steps too. For this purpose search the internet.



回答2:

You may want to try some pre-built binaries. Unfortunately, the installer is an MSI, so installation using them with virtualenv would not be possible.

I believe what you're referring to with the ora file is the TNS names file. If so, it's a plain text file with a particular format. This might help: http://www.orafaq.com/wiki/Tnsnames.ora. According to the FAQ, Oracle will look in the TNS_ADMIN directory for the file tnsnames.ora. Note that using a TNS names file may be optional. In many cases, you can drop what you would normally put in the TNS names file (so something like (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=ORA11)))) in the same place you would a TNS name.

Welcome to Oracle. Good luck. You'll need it. (Yes, everything is this complicated with Oracle.)