I try to write a script in .py for oracle connectivity:
#!/usr/bin/python import cx_Oracle connstr='username/pwd@database' conn = cx_Oracle.connect(connstr) curs = conn.cursor() curs.execute('select * from table1;') print curs.description for row in curs: print row conn.close() I get the following error:
Traceback (most recent call last): File "test_SQLPython.py", line 3, in ? import cx_Oracle ImportError: No module named cx_Oracle Any help would be appreciated? Thanks.
110 Answers
Tried installing it via rpm posted in above answers, but it didn't worked. What worked instead is plain pip install.
pip install cx_oracle The above command installed cx_oracle=6.1 Please note that I'm using python 2.7.14 Anaconda release and oracle 12c.
Windows help:
- Get the instant client from here.
- Put the directory into your PATH variable.
Go to the command prompt (Win+R and type
cmd) and set 2 variables matching your location- for example:set TNS_ADMIN=C:\instant_client\instantclient_11_2set ORACLE_HOME=C:\instant_client\instantclient_11_2
Then install the cx_Oracle module from an exe. If you use pip or easy_install, ...good luck.
You can get the installer here:
2For me the problem was that I had installed cx_Oracle via DOS pip which changed it to lower case. Installing it through Git Bash instead kept the mixed case.
1In my case the solution was to use:
python3 -m pip install cx_Oracle --upgrade --user instead of
pip3 install cx_Oracle It was caused by some mess in pip, so I haven't got cx_Oracle installed properly:
# python3 -m pip -V pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7) # pip3 -V pip 21.2.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6) I hope it'll save someone a few hours of life...
I have just faced the same problem. First, you need to install the appropriate Oracle client for your OS. In my case, to install it on Ubuntu x64 I have followed this instructions
Then, you need to install cx_Oracle, a Python module to connect to the Oracle client. Again, assuming you are running Ubuntu in a 64bit machine, you should type in a shell:
wget -c sudo alien -i cx_Oracle-5.0.4-11g-unicode-py27-1.x86_64.rpm This will work for Oracle 11g if you have installed Python 2.7.x, but you can download a different cx_Oracle version in To check which Python version do you have, type in a terminal:
python -V I hope it helps
1I had a similar problem, you gotta make sure you have:
- oracle instant client
- cx_Oracle binary( from SourceForge )
- Python IMPORTANT: Make sure they are ALL either 64-bit or 32-bit, mixing is gonna cause problems
Windows and Anaconda help
Anaconda 4.3.0 comes with Python 3.6 as the root. Currently cx_Oracle only supports up to 3.5. I tried creating 3.5 environment in envs, but when running cx_Oracle-5.2.1-11g.win-amd64-py3.5.exe it installs in root only against 3.6
Only workaround I could find was to change the root environment from 3.6 to 3.5:
activate root conda update --all python=3.5 When that completes run cx_Oracle-5.2.1-11g.win-amd64-py3.5.exe.
Tested it with import and worked fine.
import CX_Oracle 1To access Oracle from python you need (additionally) the cx_Oracle module. The module must be located either in the system python path or you have to set the PYTHONPATH appropriate.
Unknown92 answer helped me (on windows). Note that all versions must fit.
I've downloaded cx_Oracle here, for me the file cx_Oracle-5.2.1-12c.win-amd64-py3.5.exe worked with:
- Python 3.5.1 64bit. for some reason the default download link from the main page is for the 32bit version.
- And oracle instance client 12.1.0.2.0 (the file named instantclient-basic-windows.x64-12.1.0.2.0.zip) here.
Although silly mistake but make sure to use correct module name and respect capitalization
I installed this package via command line as pip install cx_oracle in my windows machine. While importing it in spyder as cx_oracle, it kept on giving following error:
ModuleNotFoundError: No module named 'cx_oracle'.
Upon correcting the module name in import command to cx_Oracle (i.e. capital letter 'O' in oracle), it was a successful import.