20

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.

1
  • 1
    Where is cx_Oracle.py located? It's probably somewhere which is not listed in your python PATH Commented Mar 28, 2012 at 13:18

11 Answers 11

24

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.

Sign up to request clarification or add additional context in comments.

Comments

6

CX_oracle has been deprecated in the new releases as per the below Documentation

https://cx-oracle.readthedocs.io/en/latest/api_manual/deprecations.html

so directly installing through PIP command might throw such errors , going forward you can import and use the python-oracledb module for connecting with oracle databases with below command.

python -m pip install oracledb

Also if you are using legacy codebase, you will be importing CX_oracle modules on top of the code and using it within the code, and IDE will show an error in the code, to overcome this you can simply change the imports alone in the top like shown below instead of changing the code to oracle_db.

import oracledb as cx_Oracle

1 Comment

The best of information about upgrading to the new driver version is in the doc The python-oracledb and cx_Oracle Drivers.
5

Windows help:

  1. Get the instant client from here.
  2. Put the directory into your PATH variable.
  3. 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_2 set 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: https://pypi.python.org/pypi/cx_Oracle/5.1.3

2 Comments

Can you clarify a bit? e.g. what do you mean bt "instant client"?
"insta clients" are available here oracle.com/database/technologies/instant-client/downloads.html for download.
3

For 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.

1 Comment

That's it. I reinstalled using "pip install cx_Oracle", with capital 'O', and it imported successfully. Thank you, MrDBA.
3

In 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...

1 Comment

This one worked for me, thanks.
2

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 https://help.ubuntu.com/community/Oracle%20Instant%20Client#Install_RPMs

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 http://prdownloads.sourceforge.net/cx-oracle/cx_Oracle-5.0.4-11g-unicode-py27-1.x86_64.rpm
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 http://cx-oracle.sourceforge.net/ To check which Python version do you have, type in a terminal:

python -V

I hope it helps

1 Comment

so client/driver is not included in cx_Oracle module itself?
2

I had a similar problem, you gotta make sure you have:

  1. oracle instant client
  2. cx_Oracle binary( from SourceForge )
  3. Python IMPORTANT: Make sure they are ALL either 64-bit or 32-bit, mixing is gonna cause problems

Comments

2

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

1 Comment

I have currently a conda env working with cx_Oracle using Python3.6 (only in Pycharm and not ipython) so this might have changed since the post.
2

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.

Comments

1

To 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.

Comments

1

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.

2 Comments

Great! For installation of Oracle lib, scroll down to the end of page and read the Installation section.
I was thinking, the file name looks likes like 64 bit only for AMD processors! I'm using Python 3.5 + Oracle 11g + Intel Xeon 64-bit processor. 6.1 is latest version. Intel 64 bit processors still not included even now. pypi.python.org/pypi/cx_Oracle/6.1 A quote from here: cx-oracle.readthedocs.io/en/latest/… This will download and install a pre-compiled binary if one is available for your architecture. If a pre-compiled binary is not available, the source will be downloaded, compiled, and the resulting binary installed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.