是否可靠并记录了PYTHONPATH如何填充sys.path? [英] Is it reliable and documented how PYTHONPATH populates the sys.path?
问题描述
在我的计算机上,PYTHONPATH
中的值似乎已插入sys.path
中:
On my machine, the values from PYTHONPATH
appear to get inserted in sys.path
:
- 从索引1开始
- 保留的订单
- 重复数据删除
例如,使用PYTHONPATH=/spam:/eggs:/spam
,然后检入python -m site
,我得到的结果如下:
For example, with PYTHONPATH=/spam:/eggs:/spam
and then checking in python -m site
, I get a result like:
sys.path = [
something,
'/spam',
'/eggs',
more,
stuff,
after
]
在Python 2和Python 3上,这似乎是相同的行为.问题是,对PYTHONPATH
的这种处理有多少记录在案/可靠的,如果在其他平台上可能有所不同呢?是将其烘焙到解释器中,还是由site.py
处理并/或处于被系统管理员调整"的危险中?
It seems to be the same behaviour on Python 2 and Python 3. The question is, how much of this handling of PYTHONPATH
is documented / reliable, and what if any might be different on other platforms? Is this baked into the interpreter, or is handled by site.py
and/or in danger of being "tweaked" by sysadmins?
在文档此处中,我看不到它的解释. >,它只是说sys.path
被增强"了(并且与文档相反,不存在的目录似乎没有被默默忽略).
I can't see it explained in the documentation here, it just says sys.path
is "augmented" (and, contrary to the documentation, non-existent directories do not appear to be silently ignored).
推荐答案
让我们进入列表.
- 从索引1开始
- beginning at index 1
那是可靠的.如 PYTHONPATH文档
默认搜索路径取决于安装,但通常 以prefix/lib/pythonversion开头(请参见上面的PYTHONHOME).它是 始终附加到PYTHONPATH.
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.
将在前面的搜索路径中插入一个附加目录 如上在接口选项"下所述.搜索 路径可以在Python程序中作为变量进行操作 sys.path.
An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.
在PYTHONPATH之前插入一个目录,该目录可以是当前目录,脚本目录或其他目录,具体取决于您运行Python的方式.其他目录被追加. site
模块还将向sys.path
添加一些模块,但是site
也附加:
One directory is inserted before PYTHONPATH, which may be the current directory, the script directory, or some other directory depending on how you ran Python. Other directories are appended. The site
module will also add some modules to sys.path
, but site
appends too:
导入此模块会将特定于站点的路径附加到模块搜索路径,并添加一些内置函数...
Importing this module will append site-specific paths to the module search path and add a few builtins...
- 保留的订单
- order preserved
我认为这在任何地方都没有明确记载,但是搜索路径顺序很重要,更改它是向后兼容的突破,我认为他们不会轻易做到.
I don't think this is explicitly documented anywhere, but search path order is important, and changing it is a backward compatibility break I don't think they would make lightly.
- 重复数据删除
- de-duplicated
这是site
模块的未记录效果.如果您使用-S
标志禁用site
来运行Python,则不会发生这种情况.您可以在 site.removeduppaths
中查看代码
That's an undocumented effect of the site
module. It won't happen if you run Python with the -S
flag that disables site
. You can see the code in site.removeduppaths
这篇关于是否可靠并记录了PYTHONPATH如何填充sys.path?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!