python如何加载sys.path中的所有值? [英] How does python load all the values in sys.path?

查看:70
本文介绍了python如何加载sys.path中的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,在开始的第一天,我就在玩自己的python脚本和项目(例如django)。

I'm a python beginner and I'm in my first days playing with my own python scripts and projects such as django.

我使用Ubuntu并设置我的PYTHONPATH为

I use Ubuntu and I set my PYTHONPATH as

export PYTHONPATH=/usr/lib/python2.6:~/Projects/mypython 

当我遇到python解释器时

When I run into a python interpreter

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-包/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 /本地/lib/python2.6/dist-packages、/usr/lib/python2.6/dist-packages、/usr/lib/python2.6/dist-packages/PIL、/usr/lib /蟒蛇2.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']

['', '/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路径?有索引吗?此外,我在/ usr / lib / pymodules上安装了django,即使它不在sys.path列表中,它也可以运行。

How does python load all the non-PYTHONPATH paths? Is there any index? Moreover, I have my django installed at /usr/lib/pymodules and, even it's not in the sys.path list, it runs.

非常感谢。

推荐答案

这是Python解释器在我计算机上的每个文件的列表(该文件的设置与您的文件相似,但不相同) )在开始运行我提供的代码之前先打开:

This is a list of every file the Python interpreter on my computer (which is set up similar to, but not the same as, yours) opens before it starts running code provided by me:

$ 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 之前的内容是极不可能的(这些都是所有OS库和特定于Python的接口)。我建议您仔细查看 site.py sitecustomize.py 和各种 .pth 文件(您的 .pth 文件列表与我的不同;这是正常的。)

All of these could, at least potentially, have an effect on sys.path. It is extremely unlikely that the stuff that comes before site.py would, however (those are all OS libraries and interfaces not specific to Python). I'd suggest you take a hard look at site.py, sitecustomize.py, and the various .pth files (your list of .pth files will be different than mine; that's normal).

这篇关于python如何加载sys.path中的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆