Home > Database > Mysql Tutorial > body text

Python中cx_Oracle模块安装遇到的问题与解决方法

WBOY
Release: 2016-06-07 17:00:13
Original
1150 people have browsed it

安装或使用cx_Oracle时,需要用到Oracel的链接库,如libclntsh.so.10.1,否则会有各种各样的错误信息。安装Oracle Instant Clien

安装或使用cx_Oracle时,,需要用到Oracel的链接库,如libclntsh.so.10.1,否则会有各种各样的错误信息。

安装Oracle Instant Client就可得到这个链接库,避免安装几百兆之巨的Oracle Client。

软件下载地址:

cx_Oracle的主页:

必需的Oracle链接库的下载地址:

常见的错误和解决方法:

一.win32二进制安装

在windows下安装cx_Oracle-5.0-10g.win32-py2.6.msi之后,导入时,报DLL加载失败的错误,如下:

IDLE 2.6.1     
>>> import cx_Oracle

Traceback (most recent call last):
  File "", line 1, in
    import cx_Oracle
ImportError: DLL load failed: 找不到指定的程序。

 

解决方法:

    从Oracle站点下载instantclient-basic-win32-10.2.0.4.zip,解压后,将其中的oci.dll文件复制到python安装目录的Lib\site-packages下,如 C:\Python26\Lib\site-packages

二.linux下二进制安装

在linux_x86_64下,安装cx_Oracle-5.0.1-10g-py24-1.x86_64.rpm时报错。

[root@BJ-UPDATE-01 ~]# rpm -ivh cx_Oracle-5.0.1-10g-py24-1.x86_64.rpm
error: Failed dependencies:
        libclntsh.so.10.1()(64bit) is needed by cx_Oracle-5.0.1-1.x86_64

解决方法:

参考

从Oracle站点下载basic-10.2.0.4.0-linux-x86_64.zip到/opt目录并解压,会看到libclntsh.so.10.1在/opt/instantclient_10_2目录中

设置环境变量

vi /root/.bash_profile

增加如下两行:

export ORACLE_HOME=/opt/instantclient_10_2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
运行source /root/.bash_profile使改动生效

建立此链接库的符号链接

cd $ORACLE_HOME

ln -s libclntsh.so.x.x libclntsh.so

重新安装cx_Oracle

注意加--nodeps参数,否则还会报上述错误

[root@BJ-UPDATE-01 ~]# rpm -ivh --nodeps cx_Oracle-5.0.1-10g-py24-1.x86_64.rpm 

#5.0.3版本不用加--nodeps参数

三.linux下源代码安装

设置环境变量及符号链接如上,在linux_x86_64下源代码安装,运行python setup.py build,编译时错误信息如下: 

Connection.c:1169: 警告:语句不起作用
Connection.c:1171: 错误:‘udt_Connection’ 没有名为 ‘environment’ 的成员
Connection.c:1172: 警告:传递参数 1 (属于 ‘Environment_CheckForError’)时在不兼容的指针类型间转换
Connection.c:1172: 警告:传递参数 2 (属于 ‘Environment_CheckForError’)时在不兼容的指针类型间转换
Connection.c:1172: 错误:提供给函数 ‘Environment_CheckForError’ 的实参太多
Connection.c:1176: 错误:‘udt_Connection’ 没有名为 ‘sessionHandle’ 的成员

解决方法:

5.0.3版本的未出现此错误,要注意ORACLE_HOME下要有include目录,这个目录中要有编译需要的源文件,源文件在Oracle Instant Client这个客户端中没有包含。我是从windows客户端的D:\oracle\product\10.2.0\client_1\oci\include这个目录中拷贝的。


四.import错误

>>> import cx_Oracle
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: /usr/lib/oracle/10.2.0.4/client64//lib/libnnz10.so: cannot restore segment prot after reloc: Permission denied
>>> import cx_Oracle
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: /usr/lib/oracle/10.2.0.4/client64//lib/libclntsh.so.10.1: cannot restore segment prot after reloc: Permission denied
>>> import cx_Oracle
>>>

这是因为SELinux的限制,运行如下命令取消限制:

chcon -t texrel_shlib_t cd $ORACLE_HOME/libnnz10.so

chcon -t texrel_shlib_t cd $ORACLE_HOME/libclntsh.so.10.1


五、运行时错误

Traceback (most recent call last):
  File "oracle_conn.py", line 9, in ?
    connection = cx_Oracle.Connection(u"oracle/oracle123@CCIP")
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

还是SELinux的限制,设置SELinux为disabled

   关掉selinux:  

   运行命令:  vim /etc/selinux/config
   将selinux=enforcing或permissive改成disabled
   运行命令:  setenforce 0

六. UNICODE的安装包有问题

Traceback (most recent call last):
  File "./oracle_conn.py", line 22, in ?
    folderIds=cursor.fetchmany(10)
cx_Oracle.DatabaseError: OCI-22061: Message 22061 not found; No message file for product=RDBMS, facility=OCI; arguments: [T

目前发现5.0.3版的包在执行SQL时会导致报错,不建议使用,换成非UNICODE的包就没问题了。

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!