我正在尝试为 Python 2.7.2 和 Oracle 11g 安装构建 cx_Oracle,但构建的 cx_Oracle.so 找不到 libclntsh.so.11.1,因此在 Python 中导入 cx_Oracle 失败。
/mypath/cx_Oracle-5.1.1/build/lib.linux-x86_64-2.7-11g]$ ldd cx_Oracle.so
libclntsh.so.11.1 => not found
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002ae9be290000)
libc.so.6 => /lib64/libc.so.6 (0x00002ae9be4ab000)
/lib64/ld-linux-x86-64.so.2 (0x000000389b600000)
我的 Oracle 客户端安装目录中有libclntsh.so.11.1 :
/apps/oracle/client/11.2.0.1/home1/lib]$ ls -l libclntsh.so*
libclntsh.so -> /apps/oracle/client/11.2.0.1/home1/lib/libclntsh.so.11.1
libclntsh.so.11.1
cx_Oracle setup.py 正在挑选这个 lib 目录:
/mypath/cx_Oracle-5.1.1]$ python2.7 setup.py build
/apps/oracle/client/11.2.0.1/home1/
running build
running build_ext
building 'cx_Oracle' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/apps/oracle/client/11.2.0.1/home1/rdbms/demo -I/apps/oracle/client/11.2.0.1/home1/rdbms/public -I/apps/bweb/python-2.7.2/include/python2.7 -c cx_Oracle.c -o build/temp.linux-x86_64-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.1.1
In file included from /apps/oracle/client/11.2.0.1/home1/rdbms/public/oci.h:3024,
from cx_Oracle.c:10:
/apps/oracle/client/11.2.0.1/home1/rdbms/public/ociap.h:10788: warning: function declaration isn't a prototype
/apps/oracle/client/11.2.0.1/home1/rdbms/public/ociap.h:10794: warning: function declaration isn't a prototype
gcc -pthread -shared build/temp.linux-x86_64-2.7-11g/cx_Oracle.o -L/apps/oracle/client/11.2.0.1/home1/lib -lclntsh -o build/lib.linux-x86_64-2.7-11g/cx_Oracle.so
这个设置明显有问题吗?
谢谢
更新
我的LD_LIBRARY_PATH包含上面带有libclntsh.so.11.1的 lib 目录
$ echo $LD_LIBRARY_PATH
/apps/oracle/client/11.2.0.1/lib
这似乎没有任何区别。我重建了 cx_Oracle.so 文件,它仍然libclntsh.so.11.1 => not found
在我运行时显示$ ldd cx_Oracle.so
。
Python 无法加载构建的模块:
Python 2.7.2 (default, Jan 19 2012, 14:38:32)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] 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: libclntsh.so.11.1: cannot open shared object file: No such file or directory
解决了
该问题与LD_LIBRARY_PATH环境变量有关。由于我正在使用的设置的限制(corp env),我不得不将 cx_Oracle 构建为另一个用户(系统帐户)。即我正在运行这个:
$ sudo -u username python27 setup.py build
因此,即使为我正确设置了LD_LIBRARY_PATH,当以其他用户身份执行命令时,也没有使用我的版本。通过将源代码移动到我有权限的位置并以我的用户身份运行构建,我能够成功构建。