Python cx_Oracle客户端库无法加载。

5

我尝试连接Oracle数据库,但Python脚本无法找到cx_Oracle。以下是我的脚本:

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

我遇到了以下错误:

================= 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

以下是我的操作系统和版本信息:

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

以下是我安装 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

能否有人告诉我,他们认为这个错误的来源是什么?

4个回答

5
在DPI-1047错误中的说明URL显示将PATH设置为Instant Client库的位置。此外,您需要正确的VS Redistributable - 所有这些都在说明中给出。请注意,他们没有提到设置ORACLE_HOME。
确保Instant Client与Python是相同的32位或64位架构。
cx_Oracle安装说明位于https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html 在Windows上使用cx_Oracle 8(以及macOS,但不是Linux),您可以利用init_oracle_client()来指定Instant Client库的位置。这是代替设置PATH的方法。

那是正确的答案。 - hygull
https://github.com/oracle/python-cx_Oracle/issues/362 - webjockey

2
当我设置我的Python环境时,我在Windows上添加了instantclient文件到我的PATH变量中,我甚至没有定义ORACLE_HOME的任何内容。将其添加到PATH可能有助于这个过程。
但是当您安装whl文件时,是否没有出现错误或消息?
您能否通过pip安装cx_Oracle模块?cx_Oracle网站列出的命令为“python -m pip install cx_Oracle”,但如果您正确设置了PATH变量,则应该能够使用“pip3 install cx_Oracle”进行安装。
您是否在$Python/Lib/site-packages/目录中看到cx_Oracle?如果找不到您的site packages,请使用IDLE中的此选项来查找它所在的位置。
>>> import site
>>> site.getsitepackages()
['C:\\Python36', 'C:\\Python36\\lib\\site-packages']

1
好的提示。顺便说一下,使用cx_Oracle 6安装正在改变(变得更好),我不会在安装时看到任何Oracle错误。(DPI错误消息是v6被使用的线索。)如果在运行时找不到Oracle客户端和匹配的VS可再发行版,则会显示DPI-1047,就像原帖作者看到的那样。 - Christopher Jones

0

-1

按照以下步骤操作即可:

macOS ODPI-C需要Oracle客户端库,这些库可以在Oracle Instant Client for macOS中找到。

在macOS上,ODPI-C首先使用标准库搜索顺序搜索名为“libclntsh.dylib”的库。如果没有找到,则会搜索“libclntsh.dylib.18.1”、“libclntsh.dylib.12.1”然后是“libclntsh.dylib.11.1”,最后返回错误。

Oracle Instant Client Zip¶ 要使用Oracle Instant Client zip文件运行ODPI-C应用程序:

从此处下载18、12或11.2的“Basic”或“Basic Light”zip文件。选择与您的应用程序架构匹配的64位或32位软件包。大多数应用程序使用64位。

将软件包解压缩到一个对您的应用程序可访问的单个目录中。例如:

mkdir -p /opt/oracle

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

添加链接到$HOME/lib或/usr/local/lib以使应用程序能够找到库。例如:

mkdir ~/lib

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

或者,复制所需的OCI库。例如:

mkdir ~/lib

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

对于Instant Client 11.2,必须复制OCI库。例如:

mkdir ~/lib cp /opt/oracle/instantclient_11_2/{libclntsh.dylib.11.1,libnnz11.dylib,libociei.dylib} ~/lib/ 如果您打算与Instant Client共存可选的Oracle配置文件,如tnsnames.ora、sqlnet.ora或oraaccess.xml,则创建一个network/admin子目录(如果不存在)。例如:

mkdir -p /opt/oracle/instantclient_12_2/network/admin 这是与此Instant Client链接的应用程序的默认Oracle配置目录。

或者,Oracle配置文件可以放置在另一个可访问的目录中。然后将环境变量TNS_ADMIN设置为该目录名称。


请不要复制文档,而是直接指向它。并且指向实际的Python cx_Oracle文档,而不是嵌入式库(ODPI-C)。并且指向提问者所询问的操作系统的文档。 - Christopher Jones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接