Python: 导入cx_Oracle时出现ImportError: No module named cx_Oracle错误

17

我尝试编写一个.py脚本用于Oracle连接:

#!/usr/bin/python

import cx_Oracle

connstr='username/pwd@database'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

curs.execute('select * from table1;')
print curs.description
for row in curs:
   print row
conn.close()

我遇到了以下错误:

Traceback (most recent call last):
  File "test_SQLPython.py", line 3, in ?
    import cx_Oracle
ImportError: No module named cx_Oracle

有任何帮助将不胜感激。谢谢。


1
cx_Oracle.py 位于哪里?它可能在您的 Python PATH 中未列出的某个地方。 - Niek de Klein
11个回答

1
CX_oracle在新版本中已被弃用,根据下面的文档说明。

https://cx-oracle.readthedocs.io/en/latest/api_manual/deprecations.html

所以直接通过PIP命令安装可能会出现这些错误,今后您可以使用以下命令导入和使用python-oracledb模块来连接Oracle数据库。
python -m pip install oracledb

如果你正在使用遗留的代码库,你需要在代码顶部导入CX_oracle模块并在代码中使用它。然而,IDE会显示代码中的错误。为了解决这个问题,你只需简单地修改顶部的导入语句,如下所示,而无需改变代码中的oracle_db部分。
import oracledb as cx_Oracle

关于升级到新的驱动程序版本的最佳信息在文档The python-oracledb and cx_Oracle Drivers中。 - Christopher Jones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接