什么用 Python 设置 sys.path,什么时候设置? [英] What sets up sys.path with Python, and when?

查看:21
本文介绍了什么用 Python 设置 sys.path,什么时候设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我跑步时

导入系统打印 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 环境变量的 PATH.

我可以使用代码添加更多路径

sys.path.insert(0, MORE_PATH)

  • 哪些例程设置了这些路径,何时设置?
  • 某些路径是否内置于 python 源代码中?
  • 是否有可能忽略使用sys.path.insert"插入的路径?我对此很好奇,与 mod_wsgi 一样,我发现sys.path.insert"找不到路径.我为此询问了另一篇文章问题.

添加

根据迈克尔的回答,我查看了site.py,得到了以下代码.

def addsitepackages(known_paths):"""将站点包(可能还有站点 python)添加到 sys.path"""站点目录 = []看过 = []对于前缀中的前缀:如果不是前缀或前缀:继续看到.附加(前缀)如果 sys.platform 在 ('os2emx', 'riscos'):sitedirs.append(os.path.join(prefix, "Lib", "site-packages"))elif sys.platform == 'darwin' 和 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 源代码中.

解决方案

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

另外,一些框架(比如我认为的 Django)会在启动时修改 sys.path 以满足他们的要求.

site 模块有一个很好的文档,注释源代码 并打印出一些信息,如果你通过 python -m site 运行它.

When I run

import sys 
print sys.path

on my Mac (Mac OS X 10.6.5, Python 2.6.1), I get the following results.

/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

They are grouped into 5 categories.

  • /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
  • PATH from PYTHONPATH environment variable.

And I can add more paths using the code

sys.path.insert(0, MORE_PATH)

  • What routines sets up those paths, and when?
  • Are some of the paths are built in python source code?
  • Is it possible that the paths inserted with 'sys.path.insert' are ignored? I'm curious about this, as with mod_wsgi, I found the paths are not found with 'sys.path.insert'. I asked another post for this question.

ADDED

Based on Michael's answer, I looked into site.py, and I got the following code.

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"))

I also think that the directory name that has site.py (/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6 for my Mac) should be built into Python source code.

解决方案

Most of the stuff is set up in Python's site.py which is automatically imported when starting the interpreter (unless you start it with the -S option). Few paths are set up in the interpreter itself during initialization (you can find out which by starting python with -S).

Additionally, some frameworks (like Django I think) modify sys.path upon startup to meet their requirements.

The site module has a pretty good documentation, a commented source code and prints out some information if you run it via python -m site.

这篇关于什么用 Python 设置 sys.path,什么时候设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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