import sys
print sys.path
在我的Mac上(Mac OS X 10.6.5,Python 2.6.1),我得到以下结果。
/Library/Python/2.6/site-packages/ply-3.3-py2.6.egg ... /Library/Python/2.6/site-packages/ipython-0.10.1-py2.6.egg /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6 /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload /Library/Python/2.6/site-packages /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode
它们被分成了5类:
- /Library/Python/2.6/site-packages/*.egg
- /Library/Python/2.6/site-packages
- Frameworks/Python.framework/Versions/2.6/lib/python2.6
- Frameworks/Python.framework/Versions/2.6/Extras/lib/python
- 从PYTHONPATH环境变量中得到的路径。
我可以使用以下代码添加更多路径:
sys.path.insert(0, MORE_PATH)
- 是哪些例程设置了这些路径,何时设置?
- 是否有一些路径是在Python源代码中构建的?
- 插入 'sys.path.insert' 的路径是否有可能被忽略?我很好奇,因为使用mod_wsgi时,我发现这些路径用 'sys.path.insert' 找不到。我在 另一个帖子 中提出了这个问题。
补充
根据Michael的回答,我查看了site.py文件,并得到以下代码。
def addsitepackages(known_paths):
"""Add site-packages (and possibly site-python) to sys.path"""
sitedirs = []
seen = []
for prefix in PREFIXES:
if not prefix or prefix in seen:
continue
seen.append(prefix)
if sys.platform in ('os2emx', 'riscos'):
sitedirs.append(os.path.join(prefix, "Lib", "site-packages"))
elif sys.platform == 'darwin' and prefix == sys.prefix:
sitedirs.append(os.path.join("/Library/Python", sys.version[:3], "site-packages"))
我也认为具有site.py的目录名称(/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6 对于我的Mac)应该嵌入Python源代码中。
seen
不是一个集合? :P - Mr_and_Mrs_D