1安装oracle客户端(win)
从官网下载instantclient-basic-windows.x64-18.5.0.0.0dbru.zip
下载的客户端与你要连接的数据库版本需一致
例如数据库是18c,对应下载的win-64-18.5
配置环境变量(复制客户端的dll文件到python可以不配置,但是如果报错还是配置上吧)
PATH: D:\Program\oracle\instantclient_18_5
ORACLE_HOME: D:\Program\oracle\instantclient_18_5
LD_LIBRARY_PATH:D:\Program\oracle\instantclient_18_5
2安装cx_oracle包
下载cx_Oracle-8.3.0-cp37-cp37m-win_amd64.whl
下载对应的python版本
$ pip install cx_Oracle-8.3.0-cp37-cp37m-win_amd64.whl
![](https://i-blog.csdnimg.cn/blog_migrate/5d8ed60debc89a27fabe1897d1287a74.png)
3测试连接数据库
tns = '127.0.0.1:1521/orcl'
uname = 'orale'
upwd = '123123'
db = cx_Oracle.connect(uname, upwd, tns)
print(db.version)
sql1 = 'select * from user'
df1 = pd.read_sql(sql1, db)
print(df1)
![](https://i-blog.csdnimg.cn/blog_migrate/a520a64c4e02799fc82578bbb405c9ad.png)
4常见问题
cx_Oracle.DatabaseError:DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory".
cx_Oracle.DatabaseError: DPI-1072: the Oracle Client library version is unsupported
cx_Oracle.DatabaseError: DPI-1050: Oracle Client library must be at version 11.2 or higher
![](https://i-blog.csdnimg.cn/blog_migrate/be8adf62605ff796f0c331f382290320.png)
![](https://i-blog.csdnimg.cn/blog_migrate/50b79882e57e73c7b1a8942974d69b38.png)
解决方案:
(1)检查环境变量 LD_LIBRARY_PATH是否配置
![](https://i-blog.csdnimg.cn/blog_migrate/41bb0fb3dbe9c8738c66b943ceead94e.png)
(2)下载好的oracle客户端版本中所有的dll文件复制到Python\Python37\Lib\site-packages文件夹中。如果是虚拟环境,放到虚拟环境的site-packages下。如果还不行把dll也放在base环境的python下。
![](https://i-blog.csdnimg.cn/blog_migrate/274feb132bf65fcde0318257500af4c0.png)