简体   繁体   中英

Python cx_Oracle Client Library Cannot Be Loaded

I am trying to connect to an Oracle database but python script cannot find cx_Oracle. Here is my script:

import cx_Oracle
con = cx_Oracle.connect(‘DBNAME/testitout@www.xx.yy.zz:1521/yeppers’)
print(con.version)
con.close()

This is the error I get:

================= RESTART: C:\Python35\Connect_To_Oracle.py =================
Traceback (most recent call last):
  File "C:\Python35\Connect_To_Oracle.py", line 1, in <module>
   import cx_Oracle
cx_Oracle.DatabaseError: DPI-1047: Oracle Client library cannot be loaded: The specified module could not be found. See https://oracle.github.io/odpi/doc/installation.html for help

Here is my O/S and version info:

  • Intel Xeon CPU E7-4870 @ 2.40GHz
  • Windows Server 2008 R2 Enterprise
  • Python 3.5

Here is what I did to install cx_Oracle:

1. Download Instant Client (Basic Client) from Oracle here : http://www.oracle.com/technetwork/topics/winx64soft-089540.html .
2. Unzip.
3. I added a new System Variable called ORACLE_HOME and pointed it to c:\Down\InstantClient , which is where I unzipped the above.  This is what I downloaded: instantclient-basic-windows.x64-12.2.0.1.0
4. You have to download the whl file from here: https://pypi.python.org/pypi/cx_Oracle/  To do this, you need to know your version of Python and your type of processor.  
5. You download into the scripts folder and then run pip install wheelfilename.whl

Can someone please let me know what they think that error stems from?

The instructions URL in the DPI-1047 error show to set PATH to the location of the Instant Client libraries. Also you need the correct VS Redistributable - all given in the instructions. Note they don't mention setting ORACLE_HOME.

Make sure Instant Client is the same 32-bit or 64-bit architecture as Python is.

The cx_Oracle installation instructions are at https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html

With cx_Oracle 8 on Windows (and macOS, but not Linux), you can make use of init_oracle_client() to say where the Instant Client libraries are. This is instead of setting PATH.

When I was setting up my python environment I added the instantclient files within my PATH variable for a windows box, I don't even have anything for ORACLE_HOME defined. Adding it to your PATH might help with the process.

But when you installed the whl file there was no errors or messages?

Are you able to install the cx_Oracle module via pip instead? The cx_Oracle websites lists the command as python -m pip install cx_Oracle but I believe you should be able to do it with pip3 install cx_Oracle if you have your PATH variable setup correctly.

Do you see cx_Oracle in your $Python/Lib/site-packages/ directory? If you can't find your site packages use this within your IDLE to find where it is.

>>> import site
>>> site.getsitepackages()
['C:\\Python36', 'C:\\Python36\\lib\\site-packages']

I solved the problem by downloading the oracle instant client from https://www.oracle.com/technetwork/topics/winx64soft-089540.html and adding the folder's location to windows path. Worked fine for me!

Folllow below steps it will work macOS ODPI-C requires Oracle Client libraries, which are found in Oracle Instant Client for macOS.

On macOS, ODPI-C first searches for a library called “libclntsh.dylib” using the standard library search order. If this is not found, it will then search for “libclntsh.dylib.18.1”, “libclntsh.dylib.12.1” and then for “libclntsh.dylib.11.1” before returning an error.

Oracle Instant Client Zip¶ To run ODPI-C applications with Oracle Instant Client zip files:

Download the 18, 12, or 11.2 “Basic” or “Basic Light” zip file from here. Choose either a 64-bit or 32-bit package, matching your application architecture. Most applications use 64-bit.

Unzip the package into a single directory that is accessible to your application. For example:

mkdir -p /opt/oracle

unzip instantclient-basic-macos.x64-12.2.0.1.0.zip

Add links to $HOME/lib or /usr/local/lib to enable applications to find the library. For example:

mkdir ~/lib

ln -s /opt/oracle/instantclient_12_2/libclntsh.dylib ~/lib/

Alternatively, copy the required OCI libraries. For example:

mkdir ~/lib

cp /opt/oracle/instantclient_12_2/{libclntsh.dylib.12.1,libclntshcore.dylib.12.1,libons.dylib,libnnz12.dylib,libociei.dylib} ~/lib/

For Instant Client 11.2, the OCI libraries must be copied. For example:

mkdir ~/lib cp /opt/oracle/instantclient_11_2/{libclntsh.dylib.11.1,libnnz11.dylib,libociei.dylib} ~/lib/ If you intend to co-locate optional Oracle configuration files such as tnsnames.ora, sqlnet.ora or oraaccess.xml with Instant Client, then create a network/admin subdirectory, if it does not exist. For example:

mkdir -p /opt/oracle/instantclient_12_2/network/admin This is the default Oracle configuration directory for applications linked with this Instant Client.

Alternatively, Oracle configuration files can be put in another, accessible directory. Then set the environment variable TNS_ADMIN to that directory name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM