Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How python load *.so when import a module

I am using a python package name cx_Oracle, it's depends oracle instantclient dynamic shared library 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)

And I'm set LD_LIBRARY_PATH to oracle instantclient's home in bash, It's works fine:

[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
>>>

But when set LD_LIBRARY_PATH in python, it doesn't work:

[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
>>>

I'm curious about how python load the libclntsh.so.11.1, and how can I load the file in python instead of in bash.

like image 783
Xiwei Wang Avatar asked Sep 05 '26 00:09

Xiwei Wang


1 Answers

You cannot set LD_LIBRARY_PATH environment variable from inside of a process that is loading modules that requires it. It needs to have been set in the process environment of the parent process variables before executing the application.

like image 93
Graham Dumpleton Avatar answered Sep 07 '26 13:09

Graham Dumpleton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!