Python "DPI-1047 Cannot locate dlopen(libclntsh.dylib)" 在 macOS 上

Python "DPI-1047 Cannot locate dlopen(libclntsh.dylib)" on macOS

我收到以下错误。

cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "dlopen(libclntsh.dylib, 1): image not found".

我发现的一个修复方法是插入 cx_Oracle.init_oracle_client(lib_dir=path) 导致 Oracle Client Library has already been initialized 错误。

但是我没有在代码中的任何地方调用 init_oracle_client。我在 Mac.

上使用 Python 3.8.11 和 cx_Oracle 8.2.1

当我最初 运行 代码时,我没有安装任何 Oracle 客户端。然后我下载了 instantclient_19_8,但是将它与 init_oracle_client 一起使用会导致已经初始化的错误。 我在这里 https://cx-oracle.readthedocs.io/en/latest/user_guide/initialization.html.

看到了这个

我不确定 cx_Oracle 二进制文件在哪里,当我转到具有当前 anaconda 环境的位置时,我看到一个 cx_Oracle-doc 文件夹,其中包含自述文件。此外,在 ~ 中创建了一个 oradiag_my_username 文件夹。不确定需要修复什么。

我的代码总是这样做:

import os
import platform
if platform.system() == "Darwin":
    cx_Oracle.init_oracle_client(lib_dir=os.environ.get("HOME")+"/Downloads/instantclient_19_8")

这是最方便的解决方案。如果您收到 'already initialized' 错误,请确保每个 Python 进程只调用 init_oracle_client() 一次

或者您可以找到您的 cx_Oracle 二进制文件,例如:

cjones@mac:~$ python
Python 3.9.6 (default, Aug 20 2021, 13:36:17) 
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> cx_Oracle
<module 'cx_Oracle' from '/Users/cjones/.local/lib/python3.9/site-packages/cx_Oracle.cpython-39-darwin.so'>

然后,在终端 window 中,执​​行如下操作:

ln -s $HOME/Downloads/instantclient_19_8/libclntsh.dylib $HOME/.local/lib/python3.9/site-packages

这是针对 macOS 的 - 任何阅读本文的 Linux 用户都应该知道此解决方案不适用于 Linux。

oradiag_xxx 目录用于 Oracle“客户端”跟踪。您可以随时删除它。