Python cx_Oracle 客户端库无法加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45225918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:50:17  来源:igfitidea点击:

Python cx_Oracle Client Library Cannot Be Loaded

pythonoracle

提问by user8340926

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

我正在尝试连接到 Oracle 数据库,但 python 脚本找不到 cx_Oracle。这是我的脚本:

import cx_Oracle
con = cx_Oracle.connect(‘DBNAME/[email protected]: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
  • 英特尔至强 CPU E7-4870 @ 2.40GHz
  • Windows Server 2008 R2 企业版
  • 蟒蛇 3.5

Here is what I did to install cx_Oracle:

这是我为安装 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?

有人可以让我知道他们认为错误源于什么吗?

回答by Christopher Jones

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.

DPI-1047 错误中的说明 URL 显示将 PATH 设置为 Instant Client 库的位置。您还需要正确的 VS Redistributable - 所有这些都在说明中给出。请注意,他们没有提到设置 ORACLE_HOME。

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

确保 Instant Client 与 Python 具有相同的 32 位或 64 位架构。

回答by stephen

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.

当我设置 python 环境时,我在 Windows 框的 PATH 变量中添加了 Instantclient 文件,我什至没有为 ORACLE_HOME 定义任何内容。将它添加到您的 PATH 可能有助于该过程。

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

但是当您安装 whl 文件时,没有错误或消息?

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_Oraclebut I believe you should be able to do it with pip3 install cx_Oracleif you have your PATH variable setup correctly.

您可以通过 pip 安装 cx_Oracle 模块吗?cx_Oracle 网站列出了该命令,python -m pip install cx_Oracle但我相信pip3 install cx_Oracle如果您正确设置了 PATH 变量,您应该可以使用它。

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.

您在 $Python/Lib/site-packages/ 目录中看到 cx_Oracle 了吗?如果您找不到您的站点包,请在您的 IDLE 中使用它来查找它的位置。

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

回答by Abhishek Poudel

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

我通过从https://www.oracle.com/technetwork/topics/winx64soft-089540.html下载 oracle 即时客户端并将文件夹的位置添加到 windows 路径解决了这个问题。对我来说很好用!

回答by pkhauzhal

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

按照以下步骤操作,macOS ODPI-C 需要 Oracle 客户端库,可在适用于 macOS 的 Oracle Instant Client 中找到。

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.

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

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

甲骨文即时客户端邮编?要使用 Oracle Instant Client zip 文件运行 ODPI-C 应用程序:

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.

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

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

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

mkdir -p /opt/oracle

mkdir -p /opt/oracle

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

解压 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:

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

mkdir ~/lib

mkdir ~/lib

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

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

Alternatively, copy the required OCI libraries. For example:

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

mkdir ~/lib

mkdir ~/lib

cp /opt/oracle/instantclient_12_2/{libclntsh.dylib.12.1,libclntshcore.dylib.12.1,libons.dylib,libnnz12.dylib,libociei.dylib} ~/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:

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

mkdir ~/libcp /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 ~/lib cp /opt/oracle/instantclient_11_2/{libclntsh.dylib.11.1,libnnz11.dylib,libociei.dylib} ~/lib/如果您打算共同定位可选的 Oracle 配置文件,例如 tnsnames.ora、sqlnet。 ora 或 oraaccess.xml 与 Instant Client,然后创建一个 network/admin 子目录(如果它不存在)。例如:

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

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

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

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