我跑的时候
import sys
print sys.path
Run Code Online (Sandbox Code Playgroud)
在我的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类.
我可以使用代码添加更多路径
sys.path.insert(0, MORE_PATH)
Run Code Online (Sandbox Code Playgroud)
根据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"))
Run Code Online (Sandbox Code Playgroud)
我还认为具有site.py的目录名(我的Mac的/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6)应该内置到Python源代码中.
And*_*ark 12
从学习Python:
sys.path是模块搜索路径.Python在程序启动时配置它,自动合并顶级文件的主目录(或空字符串以指定当前工作目录),任何PYTHONPATH目录,您创建的任何.pth文件路径的内容,以及标准库目录.结果是Python在每次导入新文件时搜索的目录名称字符串列表.
site.py确实是答案.我想删除我的mac上默认安装的旧Python的任何依赖项.这非常好用,因为每次启动python解释器时都会调用"site.py".
对于Mac,我在/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/site.py中的main()末尾手动添加了以下行:
sys.path = filter (lambda a: not a.startswith('/System'), sys.path)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54120 次 |
| 最近记录: |