什么用Python设置sys.path,什么时候?

2024-05-17 19:43:41 发布

您现在位置:Python中文网/ 问答频道 /正文

当我跑的时候

import sys 
print sys.path

在我的Mac上(MacOSX10.6.5,Python2.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包/*.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'找到的。我问了another post这个问题。

增加

根据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 for my Mac)构建到Python源代码中。


Tags: path路径extrasprefixegglibpackagessys
3条回答

学习Python

sys.path is the module search path. Python configures it at program startup, automatically merging the home directory of the top-level file (or an empty string to designate the current working directory), any PYTHONPATH directories, the contents of any .pth file paths you've created, and the standard library directories. The result is a list of directory name strings that Python searches on each import of a new file.

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)

大多数内容都是在Python的site.py中设置的,在启动解释器时会自动导入(除非您使用-S选项启动它)。在初始化过程中,解释器本身很少设置路径(您可以通过使用-S启动python来查找路径)。

另外,一些框架(我认为像Django)在启动时修改sys.path,以满足它们的需求。

site模块有一个很好的documentation,一个注释的source code,如果您通过python -m site运行它,它会输出一些信息。

相关问题 更多 >