python 如何在导入模块时加载 *.so
How python load *.so when import a module
我使用的是python包名cx_Oracle,它依赖于oracle instantclient动态共享库libclntsh.so.11.1
[wangxw@rhel7 ~]$ ldd /usr/lib64/python2.7/site-packages/cx_Oracle.so
linux-vdso.so.1 => (0x00007fffea5fe000)
libclntsh.so.11.1 => not found
libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f5c02bbe000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5c029a2000)
libc.so.6 => /lib64/libc.so.6 (0x00007f5c025e0000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f5c023db000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f5c021d8000)
libm.so.6 => /lib64/libm.so.6 (0x00007f5c01ed6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5c031c1000)
并且我在 bash 中设置 LD_LIBRARY_PATH
到 oracle instantclient 的主页,它工作正常:
[wangxw@rhel7 ~]$ export LD_LIBRARY_PATH=/home/wangxw/instantclient
[wangxw@rhel7 ~]$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
但是当在python中设置LD_LIBRARY_PATH
时,它不起作用:
[wangxw@rhel7 ~]$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import os
>>> os.environ['LD_LIBRARY_PATH'] = '/home/wangxw/instantclient'
>>> 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
>>>
我很好奇 python 如何加载 libclntsh.so.11.1
,以及如何将文件加载到 python 而不是 bash.
您不能从正在加载需要它的模块的进程内部设置 LD_LIBRARY_PATH
环境变量。它需要在执行应用程序之前在父流程变量的流程环境中设置。
我使用的是python包名cx_Oracle,它依赖于oracle instantclient动态共享库libclntsh.so.11.1
[wangxw@rhel7 ~]$ ldd /usr/lib64/python2.7/site-packages/cx_Oracle.so
linux-vdso.so.1 => (0x00007fffea5fe000)
libclntsh.so.11.1 => not found
libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f5c02bbe000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5c029a2000)
libc.so.6 => /lib64/libc.so.6 (0x00007f5c025e0000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f5c023db000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f5c021d8000)
libm.so.6 => /lib64/libm.so.6 (0x00007f5c01ed6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5c031c1000)
并且我在 bash 中设置 LD_LIBRARY_PATH
到 oracle instantclient 的主页,它工作正常:
[wangxw@rhel7 ~]$ export LD_LIBRARY_PATH=/home/wangxw/instantclient
[wangxw@rhel7 ~]$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
但是当在python中设置LD_LIBRARY_PATH
时,它不起作用:
[wangxw@rhel7 ~]$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import os
>>> os.environ['LD_LIBRARY_PATH'] = '/home/wangxw/instantclient'
>>> 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
>>>
我很好奇 python 如何加载 libclntsh.so.11.1
,以及如何将文件加载到 python 而不是 bash.
您不能从正在加载需要它的模块的进程内部设置 LD_LIBRARY_PATH
环境变量。它需要在执行应用程序之前在父流程变量的流程环境中设置。