1. 下载安装oracle instantclient
下载地址:https://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html
下载内容:
instantclient-basic-linux.x64-12.2.0.1.0.zip
instantclient-sdk-linux.x64-12.2.0.1.0.zip
将两个压缩文件合并解压到一个文件夹下
2. 安装cx_Oracle模块
1) 通过pip安装
pip3 install cx_Oracle==6.0b2
2) 下载安装
下载地址:https://sourceforge.net/projects/cx-oracle/files/
注:cx_Oracle和instantclient的选择应该与系统和oracle数据库的版本对应
如:本机系统为win64位,数据库为oracle11g时,cx_Oracle的选择如下,11g对应oracle数据库的版本,win-amd64对应操作系统版本
instantclient的选择,根据官方文档描述,可以选择如下版本
3.设置环境变量
export ORACLE_HOME=路径/instantclient_12_1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
4. 测试是否成功
python3
>>>import cx_Oracle
>>>cx_Oracle.connect
<type ‘cx_Oracle.Connection’>
5. 遇到的问题及解决
在一个非root的用户下运行:python3 -c "import cx_Oracle"时正常,但运行:sudo python3 -c "import cx_Oracle"时报错:cx_Oracle.DatabaseError: DPI-1047: Oracle Client library cannot be loaded: libclntsh.so: cannot open shared object file: No such file or directory.
解决办法:
修改linux动态链接库配置文件:sudo vim /etc/ld.so.conf
将/路径/instantclient_12_2(即instantclient解压到的目录)添加到文件里,保存
运行:sudo ldconfig
参考文章:https://app.yinxiang.com/shard/s3/nl/16541377/bc15bb54-6730-47e2-bfc3-2be6cdb1b797/