用Python设置sys.path的时间是什么时候?

时间:2022-07-12 18:18:03

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.

在我的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

They are grouped into 5 categories.

它们分为5类。

  • /Library/Python/2.6/site-packages/*.egg
  • /Library/Python/2.6/site-packages/*.egg
  • /Library/Python/2.6/site-packages
  • /Library/Python/2.6/site-packages
  • Frameworks/Python.framework/Versions/2.6/lib/python2.6
  • 框架/ Python.framework /版本/ 2.6 / lib中/ python2.6的
  • Frameworks/Python.framework/Versions/2.6/Extras/lib/python
  • 框架/ Python.framework /版本/ 2.6 /额外/ lib中/蟒蛇
  • PATH from PYTHONPATH environment variable.
  • 来自PYTHONPATH环境变量的PATH。

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?
  • 有些路径是用python源代码构建的吗?
  • 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.
  • 是否有可能忽略使用'sys.path.insert'插入的路径?我对此感到好奇,就像mod_wsgi一样,我发现'sys.path.insert'找不到路径。我问了另一个帖子这个问题。

ADDED

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

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

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.

我还认为具有site.py的目录名(我的Mac的/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6)应该内置到Python源代码中。

7 个解决方案

#1


23  

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

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

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

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

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 -m站点运行它。

#2


9  

From Learning Python:

从学习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.

sys.path是模块搜索路径。 Python在程序启动时配置它,自动合并*文件的主目录(或空字符串以指定当前工作目录),任何PYTHONPATH目录,您创建的任何.pth文件路径的内容,以及标准库目录。结果是Python在每次导入新文件时搜索的目录名称字符串列表。

#3


6  

site.py is indeed the answers. I wanted to remove any dependencies on the old Python that is installed by default on my mac. This works pretty good, as 'site.py' is called each time the python interpreter is started.

site.py确实是答案。我想删除我的mac上默认安装的旧Python的任何依赖项。这非常好用,因为每次启动python解释器时都会调用“site.py”。

For Mac, I manually added the following line at the end of main() in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/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)

#4


4  

Path has these parts:

路径有这些部分:

  • OS paths that have your system libraries
  • 具有系统库的OS路径
  • current directory python started from
  • 当前目录python从
  • environmental variable $PYTHONPATH
  • 环境变量$ PYTHONPATH
  • you can add paths at runtime.
  • 您可以在运行时添加路径。

Paths are not ignored. But, they may not be found and that will not raise an error. sys.path should only be added too, not subtracted from. Django would not remove paths.

路径不会被忽略。但是,它们可能无法找到,也不会引发错误。 sys.path也应该只添加,不能从中减去。 Django不会删除路径。

#5


1  

Adding to the accepted answer, and addressing the comments that say a module shouldn't remove entries from sys.path:

添加到已接受的答案,并解决说明模块不应从sys.path中删除条目的注释:

This is broadly true but there are circumstances where you might want to modify sys.path by removing entries. For instance - and this is Mac-specific; *nix/Windows corollaries may exist - if you create a customised Python.framework for inclusion in your own project you may want to ignore the default sys.path entries that point at the system Python.framework.

这通常是正确的,但在某些情况下您可能希望通过删除条目来修改sys.path。例如 - 这是特定于Mac的; * nix / Windows推论可能存在 - 如果您创建一个自定义的Python.framework以包含在您自己的项目中,您可能希望忽略指向系统Python.framework的默认sys.path条目。

You have a couple of options:

你有几个选择:

  1. Hack the site.py, as @damirv indicates, or

    破解site.py,如@damirv所示,或

  2. Add your own sitecustomize module (or package) to the custom framework that achieves the same end result. As indicated in the site.py comments (for 2.7.6, anyway):

    将您自己的sitecustomize模块(或包)添加到实现相同最终结果的自定义框架。如site.py评论中所示(无论如何,为2.7.6):

    After these path manipulations, an attempt is made to import a module named sitecustomize, which can perform arbitrary additional site-specific customizations. If this import fails with an ImportError exception, it is silently ignored.

    在这些路径操作之后,尝试导入名为sitecustomize的模块,该模块可以执行任意其他特定于站点的自定义。如果此导入失败并出现ImportError异常,则会以静默方式忽略该异常。

#6


1  

Also note: if the PYTHONHOME env var is set, standard libraries will be loaded from this path instead of the default, as documented.

另请注意:如果设置了PYTHONHOME env var,将从此路径加载标准库,而不是默认加载,如文档所述。

This is not a direct answer to the question, but something I just discovered that was causing the wrong standard libraries to be loaded, and my searches lead me here along the way.

这不是这个问题的直接答案,但是我刚刚发现的东西导致加载错误的标准库,我的搜索引导我一路走来。

#7


1  

You are using system python /usr/bin/python.

您正在使用system python / usr / bin / python。

sys.path is set from system files at python startup.

sys.path是从python启动时的系统文件设置的。

Do not touch those files, in particular site.py, because this may perturb the system.

请勿触摸这些文件,特别是site.py,因为这可能会扰乱系统。

However, you can change sys.path within python, in particular, at startup :

但是,您可以在python中更改sys.path,特别是在启动时:

in ~/.bashrc or ~/.zshrc:

在〜/ .bashrc或〜/ .zshrc中:

export PYTHONSTARTUP=~/.pythonrc

export PYTHONSTARTUP =〜/ .pythonrc

in ~/.pythonrc:

在〜/ .pythonrc中:

write your changes to sys.path.

将更改写入sys.path。

Those changes will be only for you in interactive shells.

这些更改仅适用于交互式shell。

For hacking at little risk for the system, install you own and more recent python version.

对于系统风险很小的黑客攻击,安装自己的和更新的python版本。

#1


23  

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

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

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

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

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 -m站点运行它。

#2


9  

From Learning Python:

从学习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.

sys.path是模块搜索路径。 Python在程序启动时配置它,自动合并*文件的主目录(或空字符串以指定当前工作目录),任何PYTHONPATH目录,您创建的任何.pth文件路径的内容,以及标准库目录。结果是Python在每次导入新文件时搜索的目录名称字符串列表。

#3


6  

site.py is indeed the answers. I wanted to remove any dependencies on the old Python that is installed by default on my mac. This works pretty good, as 'site.py' is called each time the python interpreter is started.

site.py确实是答案。我想删除我的mac上默认安装的旧Python的任何依赖项。这非常好用,因为每次启动python解释器时都会调用“site.py”。

For Mac, I manually added the following line at the end of main() in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/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)

#4


4  

Path has these parts:

路径有这些部分:

  • OS paths that have your system libraries
  • 具有系统库的OS路径
  • current directory python started from
  • 当前目录python从
  • environmental variable $PYTHONPATH
  • 环境变量$ PYTHONPATH
  • you can add paths at runtime.
  • 您可以在运行时添加路径。

Paths are not ignored. But, they may not be found and that will not raise an error. sys.path should only be added too, not subtracted from. Django would not remove paths.

路径不会被忽略。但是,它们可能无法找到,也不会引发错误。 sys.path也应该只添加,不能从中减去。 Django不会删除路径。

#5


1  

Adding to the accepted answer, and addressing the comments that say a module shouldn't remove entries from sys.path:

添加到已接受的答案,并解决说明模块不应从sys.path中删除条目的注释:

This is broadly true but there are circumstances where you might want to modify sys.path by removing entries. For instance - and this is Mac-specific; *nix/Windows corollaries may exist - if you create a customised Python.framework for inclusion in your own project you may want to ignore the default sys.path entries that point at the system Python.framework.

这通常是正确的,但在某些情况下您可能希望通过删除条目来修改sys.path。例如 - 这是特定于Mac的; * nix / Windows推论可能存在 - 如果您创建一个自定义的Python.framework以包含在您自己的项目中,您可能希望忽略指向系统Python.framework的默认sys.path条目。

You have a couple of options:

你有几个选择:

  1. Hack the site.py, as @damirv indicates, or

    破解site.py,如@damirv所示,或

  2. Add your own sitecustomize module (or package) to the custom framework that achieves the same end result. As indicated in the site.py comments (for 2.7.6, anyway):

    将您自己的sitecustomize模块(或包)添加到实现相同最终结果的自定义框架。如site.py评论中所示(无论如何,为2.7.6):

    After these path manipulations, an attempt is made to import a module named sitecustomize, which can perform arbitrary additional site-specific customizations. If this import fails with an ImportError exception, it is silently ignored.

    在这些路径操作之后,尝试导入名为sitecustomize的模块,该模块可以执行任意其他特定于站点的自定义。如果此导入失败并出现ImportError异常,则会以静默方式忽略该异常。

#6


1  

Also note: if the PYTHONHOME env var is set, standard libraries will be loaded from this path instead of the default, as documented.

另请注意:如果设置了PYTHONHOME env var,将从此路径加载标准库,而不是默认加载,如文档所述。

This is not a direct answer to the question, but something I just discovered that was causing the wrong standard libraries to be loaded, and my searches lead me here along the way.

这不是这个问题的直接答案,但是我刚刚发现的东西导致加载错误的标准库,我的搜索引导我一路走来。

#7


1  

You are using system python /usr/bin/python.

您正在使用system python / usr / bin / python。

sys.path is set from system files at python startup.

sys.path是从python启动时的系统文件设置的。

Do not touch those files, in particular site.py, because this may perturb the system.

请勿触摸这些文件,特别是site.py,因为这可能会扰乱系统。

However, you can change sys.path within python, in particular, at startup :

但是,您可以在python中更改sys.path,特别是在启动时:

in ~/.bashrc or ~/.zshrc:

在〜/ .bashrc或〜/ .zshrc中:

export PYTHONSTARTUP=~/.pythonrc

export PYTHONSTARTUP =〜/ .pythonrc

in ~/.pythonrc:

在〜/ .pythonrc中:

write your changes to sys.path.

将更改写入sys.path。

Those changes will be only for you in interactive shells.

这些更改仅适用于交互式shell。

For hacking at little risk for the system, install you own and more recent python version.

对于系统风险很小的黑客攻击,安装自己的和更新的python版本。