首页 > 代码库 > Python导入cx_Oracle报错

Python导入cx_Oracle报错

系统环境:RHEL5.4   python2.5(手动编译安装,系统带有2.4版本)

在使用python脚本访问数据库时,需要导入cx_Oracle模块    

$>>>import cx_Oracle
Traceback (most recent call last):
  File "/ghca/monitor/async_Msg_MQDis/run.py", line 2, in <module>
    import async_Msg_MQDis
  File "/ghca/monitor/async_Msg_MQDis/async_Msg_MQDis.py", line 1, in <module>
    import cx_Oracle
ImportError: No module named cx_Oracle
分析原因:
1、怀疑缺少模块包
[oracle@radius1 async_Msg_MQDis]$ rpm -qa|grep cx
cx_Oracle-5.1.2-1
发现已安装,查询包安装目录
[oracle@radius1 async_Msg_MQDis]$ rpm -ql cx_Oracle-5.1.2-1|less
/usr/lib64/python2.4/site-packages/cx_Oracle.so
/usr/share/doc/cx_Oracle-5.1.2
2、切换到模块目录下进行导入
$cd /usr/lib64/python2.4/site-packages
[oracle@radius1 site-packages]$ python
Python 2.5.2 (r252:60911, Apr 11 2013, 14:10:12) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode
>>> 
3、猜测可能是python版本与cx_Oracle对应版本不一致导入
[oracle@radius1 ~]$ ls /usr/bin/python2.4 
/usr/bin/python2.4
[oracle@radius1 ~]$ /usr/bin/python2.4
Python 2.4.3 (#1, Jun 11 2009, 14:09:37) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> 
导入成功
总结:中途也曾重新下载cx_Oracle-5.1.2-10g-py25-1.i386.rpm和cx_Oracle-5.1.2-10g-py25-1.x86_64.rpm进行尝试,均未导入成功,并且在安装了cx_Oracle*py25*.x86_64.rpm版本进行导入时报缺少“libpython2.5.so.1.0”库文件错误。在公司测试机上装有python2.6,其中 /usr/lib/libpython2.6.so.1.0 库文件属于python-libs-2.6.6-20.el6.i686包。

Python导入cx_Oracle报错