简单来说,Python运行时的包会去sys.path找,而sys.path这个数组会从前到后找,优先级以此是:
文件本身的路径系统环境变量PYTHONPATH的路径系统环境变量PATH里指定的anaconda的安装路径可以用以下脚本进行一下测试:
import osimport sysprint("os.environ['PYTHONPATH']={0}".format(os.environ['PYTHONPATH']))print("os.environ['PATH']={0}".format(os.environ['PATH']))print("sys.path={0}".format(sys.path))-----------------------------------------------------------------------
LD_LIBRARY_PATH是用来寻找Python的加载binary文件的。sys.path is only searched for Python modules. For dynamic linked libraries, the paths searched must be in LD_LIBRARY_PATH. Check if your LD_LIBRARY_PATH includes /usr/local/lib, and if it doesn't, add it and try again.
An example:
I'm trying to import pycurl:
$ python -c "import pycurl"Traceback (most recent call last):File "<string>", line 1, in <module>ImportError: libcurl.so.4: cannot open shared object file: No such file or directoryNow, libcurl.so.4 is in /usr/local/lib. As you can see, this is in sys.path:
$ python -c "import sys; print(sys.path)"['', '/usr/local/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg', '/usr/local/lib/python25.zip', '/usr/local/lib/python2.5', '/usr/local/lib/python2.5/plat-linux2', '/usr/local/lib/python2.5/lib-tk', '/usr/local/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/sitepackages', '/usr/local/lib', '/usr/local/lib/python2.5/site-packages']Any help will be greatly appreciated.
------------------------------
Answer:
sys.path is only searched for Python modules. For dynamic linked libraries, the paths searched must be in LD_LIBRARY_PATH. Check if your LD_LIBRARY_PATH includes /usr/local/lib, and if it doesn't, add it and try again.
Some more information (source):
In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories; this is useful when debugging a new library or using a nonstandard library for special purposes. The environment variable LD_PRELOAD lists shared libraries with functions that override the standard set, just as /etc/ld.so.preload does. These are implemented by the loader /lib/ld-linux.so. I should note that, while LD_LIBRARY_PATH works on many Unix-like systems, it doesn't work on all; for example, this functionality is available on HP-UX but as the environment variable SHLIB_PATH, and on AIX this functionality is through the variable LIBPATH (with the same syntax, a colon-separated list).
Update: to set LD_LIBRARY_PATH, use one of the following, ideally in your ~/.bashrc or equivalent file:
export LD_LIBRARY_PATH=/usr/local/libor
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATHUse the first form if it's empty (equivalent to the empty string, or not present at all), and the second form if it isn't. Note the use of export.