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.
This question is related to
python
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:
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:
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
I had a similar problem, you gotta make sure you have:
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.
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
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
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.
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.
Source: Stackoverflow.com