【问题标题】:Python cx_Oracle Client Library Cannot Be LoadedPython cx_Oracle 客户端库无法加载
【发布时间】:2017-12-26 19:29:50
【问题描述】:

我正在尝试连接到 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

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

  • 英特尔至强 CPU E7-4870 @ 2.40GHz
  • Windows Server 2008 R2 企业版
  • 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

有人可以告诉我他们认为错误源于什么吗?

【问题讨论】:

    标签: python oracle


    【解决方案1】:

    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(和 macOS,但不是 Linux)上使用 cx_Oracle 8,您可以使用 init_oracle_client() 来说明 Instant Client 库的位置。这不是设置 PATH。

    【讨论】:

    【解决方案2】:

    当我设置我的 python 环境时,我在我的 PATH 变量中为 windows 框添加了 Instantclient 文件,我什至没有为 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 了吗?如果找不到站点包,请在 IDLE 中使用它来查找它的位置。

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

    【讨论】:

    • 好建议。 FWIW,安装正在使用 cx_Oracle 6 更改(更好),我不希望在安装时看到任何 Oracle 错误。 (DPI 错误消息是正在使用 v6 的线索。)如果在运行时未找到 Oracle 客户端和匹配的 VS 可再发行组件,则将显示 DPI-1047,如原始海报所见。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      按照以下步骤操作即可 苹果系统 ODPI-C 需要 Oracle 客户端库,这些库位于适用于 macOS 的 Oracle Instant Client 中。

      在 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

      解压缩 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/ 如果您打算将可选的 Oracle 配置文件(例如 tnsnames.ora、sqlnet.ora 或 oraaccess.xml)与 Instant Client 放在一起,则创建一个 network/admin 子目录(如果不存在)。例如:

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

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

      【讨论】:

      • 请不要复制文档。只是指向它。并指向实际的 Python cx_Oracle 文档,而不是嵌入式库 (ODPI-C)。并指向发布者询问的操作系统的文档。
      猜你喜欢
      • 2018-06-06
      • 2019-04-05
      • 2019-01-06
      • 2018-06-25
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多