现在最简单的解决方案是使用最新的 cx_Oracle 版本,该版本已重命名为 python-oracledb,请参阅发布公告 https://cjones-oracle.medium.com/open-source-python-thin-driver-for-oracle-database-e82aac7ecf5a。这不需要 Oracle 客户端库(它们是可选的)。
如果您使用cx_Oracle(或python-oracledb的Thick模式),您可以使用如下内容:
import os
import platform
if platform.system() == "Darwin":
cx_Oracle.init_oracle_client(lib_dir=os.environ.get("HOME")+"/Downloads/instantclient_19_8")
这是最方便的解决方案。如果您收到“已初始化”错误,请确保您只调用init_oracle_client()
once如果您使用的是 cx_Oracle 8,则每个 Python 进程。如果您使用的是新版本 (重命名为 python-oracledb https://cjones-oracle.medium.com/open-source-python-thin-driver-for-oracle-database-e82aac7ecf5a),然后你可以调用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'>
然后,在终端窗口中执行以下操作:
ln -s $HOME/Downloads/instantclient_19_8/libclntsh.dylib $HOME/.local/lib/python3.9/site-packages
这是针对 macOS 的 - 任何阅读本文的 Linux 用户都应该知道该解决方案不适用于 Linux。
The oradiag_xxx
目录用于 Oracle“客户端”跟踪。您可以随时删除它。