前言
使用python3执行cx_oracle库方法无法识别oracle客户端。
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "dlopen(libclntsh.dylib, 1): image not found".
下载oracle客户端说明
官网配置说明https://oracle.github.io/odpi/doc/installation.html#id3
To run ODPI-C applications with Oracle Instant Client zip files:
1. Download the 19, 18, 12, or 11.2 “Basic” or “Basic Light” zip file from [here](http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html). Choose either a 64-bit or 32-bit package, matching your application architecture. Most applications use 64-bit.
2. Unzip the package into a single directory that is accessible to your application. For example:
mkdir -p /opt/oracle
unzip instantclient-basic-macos.x64-19.3.0.0.0dbru.zip
3. Add links to `$HOME/lib` or `/usr/local/lib` to enable applications to find the library. For example:
mkdir ~/lib
ln -s /opt/oracle/instantclient_19_3/libclntsh.dylib ~/lib/
Alternatively, copy the required OCI libraries. For example:
mkdir ~/lib
cp /opt/oracle/instantclient_19_3/{libclntsh.dylib.19.1,libclntshcore.dylib.19.1,libons.dylib,libnnz12.dylib,libociei.dylib} ~/lib/
For Instant Client 11.2, the OCI libraries must be copied. For example:
mkdir ~/lib
cp /opt/oracle/instantclient_11_2/{libclntsh.dylib.11.1,libnnz11.dylib,libociei.dylib} ~/lib/
4. 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 -p /opt/oracle/instantclient_12_2/network/admin
This is the default Oracle configuration directory for applications linked with this Instant Client.
Alternatively, Oracle configuration files can be put in another, accessible directory. Then set the environment variable `TNS_ADMIN` to that directory name.
实际操作
解压两个包,放在同一个目录
$ pwd
/work/tools/
$ unzip instantclient-basic-macos.x64-19.3.0.0.0dbru.zip
$ unzip instantclient-sqlplus-macos.x64-19.3.0.0.0dbru.zip
- 放在家目录下,这样后续python使用cx_oracle才可以正常使用
$ mkdir ~/lib
$ ln -s /work/tools/instantclient_19_3/libclntsh.dylib ~/lib/
到此就可以正常使用cx_oracle库连接oracle
- 配置环境变量
$ vi ~/.bash_profile
export ORACLE_HOME="$MYTOOL_HOME/oracle11client/instantclient_19_3"
export PATH=$ORACLE_HOME:$PATH
生效环境变量
$ . ~/.bash_profile
查看sqlplus工具
$ which sqlplus
/work/tools/oracle11client/instantclient_19_3/sqlplus