python如何加载sys.path中的所有值?
Posted
技术标签:
【中文标题】python如何加载sys.path中的所有值?【英文标题】:How does python load all the values in sys.path? 【发布时间】:2011-05-23 01:27:01 【问题描述】:我是一名 Python 初学者,我刚开始使用自己的 Python 脚本和项目,例如 django。
我使用 Ubuntu 并将我的 PYTHONPATH 设置为
export PYTHONPATH=/usr/lib/python2.6:~/Projects/mypython
当我遇到 python 解释器时
import sys
print sys.path
我可以看到
['', '/usr/bin', '/usr/local/lib/python2.6/dist-packages/ropemode-0.1_rc2-py2.6.egg', '/usr/local/lib/ python2.6/dist-packages/rope-0.9.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/ropevim-0.3_rc-py2.6.egg', ' /usr/lib/python2.6'、'/home/jaume/Projects/mypython'、'/usr/lib/python2.6/plat-linux2'、'/usr/lib/python2.6/lib-tk' , '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/ usr/lib/python2.6/dist-packages','/usr/lib/python2.6/dist-packages/PIL','/usr/lib/python2.6/dist-packages/gst-0.10',' /usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/ usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/lib/pymodules/python2.6/IPython/Extensions', u'/home/jaume/.ipython']
python 如何加载所有非 PYTHONPATH 路径?有没有索引?此外,我的 django 安装在 /usr/lib/pymodules 中,即使它不在 sys.path 列表中,它也会运行。
非常感谢。
【问题讨论】:
要了解 django 是如何加载的,请检查路径:>>> import django >>> django.__path__ ['/usr/local/lib/python2.6/dist-packages/django']
我只能看到 ['django']。也许是因为它是从 'apt-get' 安装的
【参考方案1】:
阅读:http://docs.python.org/library/site.html#module-site
【讨论】:
【参考方案2】:这是我计算机上的 Python 解释器(与您的设置类似但不一样)在开始运行我提供的代码之前打开的每个文件的列表:
$ strace -eopen python -c 1 2>&1 | grep -ve '-1 E'
open("/etc/ld.so.cache", O_RDONLY) = 3
open("/lib/libpthread.so.0", O_RDONLY) = 3
open("/lib/libdl.so.2", O_RDONLY) = 3
open("/lib/libutil.so.1", O_RDONLY) = 3
open("/usr/lib/libssl.so.0.9.8", O_RDONLY) = 3
open("/usr/lib/libcrypto.so.0.9.8", O_RDONLY) = 3
open("/usr/lib/libz.so.1", O_RDONLY) = 3
open("/lib/libm.so.6", O_RDONLY) = 3
open("/lib/libc.so.6", O_RDONLY) = 3
open("/proc/meminfo", O_RDONLY) = 3
open("/usr/lib/python2.6/site.py", O_RDONLY) = 3
open("/usr/lib/python2.6/site.pyc", O_RDONLY) = 4
open("/usr/lib/python2.6/os.py", O_RDONLY) = 4
open("/usr/lib/python2.6/os.pyc", O_RDONLY) = 5
open("/usr/lib/python2.6/posixpath.py", O_RDONLY) = 5
open("/usr/lib/python2.6/posixpath.pyc", O_RDONLY) = 6
open("/usr/lib/python2.6/stat.py", O_RDONLY) = 6
open("/usr/lib/python2.6/stat.pyc", O_RDONLY) = 7
open("/usr/lib/python2.6/genericpath.py", O_RDONLY) = 6
open("/usr/lib/python2.6/genericpath.pyc", O_RDONLY) = 7
open("/usr/lib/python2.6/warnings.py", O_RDONLY) = 6
open("/usr/lib/python2.6/warnings.pyc", O_RDONLY) = 7
open("/usr/lib/python2.6/linecache.py", O_RDONLY) = 7
open("/usr/lib/python2.6/linecache.pyc", O_RDONLY) = 8
open("/usr/lib/python2.6/types.py", O_RDONLY) = 7
open("/usr/lib/python2.6/types.pyc", O_RDONLY) = 8
open("/usr/lib/python2.6/UserDict.py", O_RDONLY) = 5
open("/usr/lib/python2.6/UserDict.pyc", O_RDONLY) = 6
open("/usr/lib/python2.6/_abcoll.py", O_RDONLY) = 6
open("/usr/lib/python2.6/_abcoll.pyc", O_RDONLY) = 7
open("/usr/lib/python2.6/abc.py", O_RDONLY) = 7
open("/usr/lib/python2.6/abc.pyc", O_RDONLY) = 8
open("/usr/lib/python2.6/copy_reg.py", O_RDONLY) = 5
open("/usr/lib/python2.6/copy_reg.pyc", O_RDONLY) = 6
open("/usr/local/lib/python2.6/dist-packages", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/usr/lib/python2.6/dist-packages", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/usr/lib/python2.6/dist-packages/PIL.pth", O_RDONLY) = 4
open("/usr/lib/python2.6/dist-packages/pygst.pth", O_RDONLY) = 4
open("/usr/lib/python2.6/dist-packages/python-support.pth", O_RDONLY) = 4
open("/usr/lib/python2.6/dist-packages/wx.pth", O_RDONLY) = 4
open("/usr/lib/python2.6/dist-packages/zope.interface-3.5.3-nspkg.pth", O_RDONLY) = 4
open("/usr/lib/python2.6/sitecustomize.py", O_RDONLY) = 4
open("/usr/lib/python2.6/sitecustomize.pyc", O_RDONLY) = 5
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
open("/usr/lib/python2.6/encodings/__init__.py", O_RDONLY) = 3
open("/usr/lib/python2.6/encodings/__init__.pyc", O_RDONLY) = 4
open("/usr/lib/python2.6/codecs.py", O_RDONLY) = 4
open("/usr/lib/python2.6/codecs.pyc", O_RDONLY) = 5
open("/usr/lib/python2.6/encodings/aliases.py", O_RDONLY) = 4
open("/usr/lib/python2.6/encodings/aliases.pyc", O_RDONLY) = 5
open("/usr/lib/python2.6/encodings/utf_8.py", O_RDONLY) = 3
open("/usr/lib/python2.6/encodings/utf_8.pyc", O_RDONLY) = 4
所有这些都可能(至少可能)对 sys.path 产生影响。然而,site.py
之前的东西极不可能出现(这些都是不特定于 Python 的操作系统库和接口)。我建议您仔细查看site.py
、sitecustomize.py
和各种.pth
文件(您的.pth
文件列表将与我的不同;这很正常)。
【讨论】:
以上是关于python如何加载sys.path中的所有值?的主要内容,如果未能解决你的问题,请参考以下文章
Python:系统变量路径已更改,与 sys.path 中的值不匹配