Python的sys.path是从哪里初始化的?
Python的sys.path是从哪里初始化的?
UPD:Python 在引用 PYTHONPATH 之前添加一些路径:
>>> import sys
>>> from pprint import pprint as p
>>> p(sys.path)
['',
'C:\\Python25\\lib\\site-packages\\setuptools-0.6c9-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\orbited-0.7.8-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\morbid-0.8.6.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\demjson-1.4-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\stomper-0.2.2-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\uuid-1.30-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\stompservice-0.1.0-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\cherrypy-3.0.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\pyorbited-0.2.2-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\flup-1.0.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\wsgilog-0.1-py2.5.egg',
'c:\\testdir',
'C:\\Windows\\system32\\python25.zip',
'C:\\Python25\\DLLs',
'C:\\Python25\\lib',
'C:\\Python25\\lib\\plat-win',
'C:\\Python25\\lib\\lib-tk',
'C:\\Python25',
'C:\\Python25\\lib\\site-packages',
'C:\\Python25\\lib\\site-packages\\PIL',
'C:\\Python25\\lib\\site-packages\\win32',
'C:\\Python25\\lib\\site-packages\\win32\\lib',
'C:\\Python25\\lib\\site-packages\\Pythonwin']
我的 PYTHONPATH 是:
PYTHONPATH=c:\testdir
我想知道 PYTHONPATH 之前的那些路径来自哪里?
Where is Python's sys.path initialized from?
UPD: Python is adding some paths before refering to PYTHONPATH:
>>> import sys
>>> from pprint import pprint as p
>>> p(sys.path)
['',
'C:\\Python25\\lib\\site-packages\\setuptools-0.6c9-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\orbited-0.7.8-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\morbid-0.8.6.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\demjson-1.4-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\stomper-0.2.2-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\uuid-1.30-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\stompservice-0.1.0-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\cherrypy-3.0.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\pyorbited-0.2.2-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\flup-1.0.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\wsgilog-0.1-py2.5.egg',
'c:\\testdir',
'C:\\Windows\\system32\\python25.zip',
'C:\\Python25\\DLLs',
'C:\\Python25\\lib',
'C:\\Python25\\lib\\plat-win',
'C:\\Python25\\lib\\lib-tk',
'C:\\Python25',
'C:\\Python25\\lib\\site-packages',
'C:\\Python25\\lib\\site-packages\\PIL',
'C:\\Python25\\lib\\site-packages\\win32',
'C:\\Python25\\lib\\site-packages\\win32\\lib',
'C:\\Python25\\lib\\site-packages\\Pythonwin']
My PYTHONPATH is:
PYTHONPATH=c:\testdir
I wonder where those paths before PYTHONPATH's ones come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
当我在 2015 年写这篇文章时,没有关于这个主题的文档。 现在有 根据评论,如果您也想查看一下。 中还有该算法的散文解释代码库中
getpath.py
的注释。 我仍然相信我的答案是相关的并且相对当前。原文如下
Python 确实努力智能地设置
sys.path< /代码>
。 怎么样
set 可以得到 真的 复杂。 以下指南是淡化的,
有点不完整,有点错误,但希望有用的指南
对于普通的Python程序员来说,当Python发生时会发生什么
找出用作
sys.path
的初始值,正常情况下的
sys.executable
、sys.exec_prefix
和sys.prefix
蟒蛇安装。
首先,Python尽最大努力弄清楚它的实际物理量
根据操作系统的指示在文件系统上的位置
它。 如果操作系统只是说“python”正在运行,它会发现自己位于 $PATH 中。
它解析任何符号链接。 一旦完成此操作,路径
它找到的可执行文件用作 sys.executable 的值,没有 ifs,
和,或但是。
接下来,它确定 sys.exec_prefix 的初始值和
sys.prefix
。如果在同一目录中有一个名为 pyvenv.cfg 的文件
sys.executable
或上一个目录,python 会查看它。 不同的操作系统对此文件执行不同的操作。
python 在此配置文件中查找的值之一是
配置选项
home =
。 Python 将使用此目录而不是包含 sys.executable 的目录当它稍后动态设置 sys.prefix 的初始值时。 如果
applocal = true
设置出现在Windows 上的
pyvenv.cfg
文件,但不是home =
设置,那么
sys.prefix
将被设置为包含sys.executable
的目录。接下来,检查
PYTHONHOME
环境变量。 在 Linux 和 Mac 上,sys.prefix
和sys.exec_prefix
设置为PYTHONHOME
环境变量,如果它存在,取代
pyvenv.cfg
中的任何home =
设置。 在 Windows 上,sys.prefix
和sys.exec_prefix
设置为PYTHONHOME
环境变量,如果存在,除非
pyvenv.cfg
中存在home =
设置,而是使用它。
否则,这些
sys.prefix
和sys.exec_prefix
是通过向后走找到的来自
sys.executable
的位置,或pyvenv.cfg
给出的home
目录(如果有)。如果在该目录中找到文件
lib/python/dyn-load
或其任何父目录,该目录被设置为
Linux 或 Mac 上的
sys.exec_prefix
。 如果文件lib/python/os.py
在目录或其任何目录中找到子目录,该目录在 Linux 上设置为 sys.prefix,
Mac 和 Windows,将
sys.exec_prefix
设置为与Windows 上的
sys.prefix
。 如果出现以下情况,在 Windows 上将跳过整个步骤:applocal = true
已设置。sys.executable
的目录是使用,或者,如果在
pyvenv.cfg
中设置了home
,则将其用于sys.prefix
的初始值。如果找不到这些“地标”文件或 sys.prefix 尚未找到
尚未找到,然后 python 将 sys.prefix 设置为“后备”
价值。 例如,Linux 和 Mac 使用预编译默认值作为
sys.prefix
和sys.exec_prefix
的值。 Windows 等待直到完全弄清楚 sys.path 来设置后备值
sys.prefix
。然后,(你们一直在等待的)python 确定初始值
将包含在
sys.path
中。sys.path
中。在 Windows 上,这始终是空字符串,它告诉 python
请改用脚本所在的完整路径。
sys.path
中,除非您在 Windows 上,
applocal
在pyvenv.cfg
中设置为 true。Windows 上的
os.path.join(os.dirname(sys.executable), "python.zip")
添加到sys.path
中。pyvenv.cfg
中未设置applocal = true
,则注册表项的子项的内容添加
HK_CURRENT_USER\Software\Python\PythonCore\\PythonPath\
(如果有)。pyvenv.cfg
中没有设置applocal = true
,并且找不到sys.prefix
,然后添加注册表项
HK_CURRENT_USER\Software\Python\PythonCore\\PythonPath\
的核心内容(如果存在);pyvenv.cfg
中未设置applocal = true
,则注册表项的子项的内容添加
HK_LOCAL_MACHINE\Software\Python\PythonCore\\PythonPath\
(如果有)。pyvenv.cfg
中没有设置applocal = true
,并且找不到sys.prefix
,然后添加注册表项
HK_CURRENT_USER\Software\Python\PythonCore\\PythonPath\
的核心内容(如果存在);添加 PYTHONPATH 的相对编译时值; 否则,该步骤将被忽略。
用于(或将用于)动态搜索 sys.prefix 的是
添加。
在 Windows 上的这个阶段,如果没有找到前缀,那么 python 将尝试
通过在 sys.path 中的所有目录中搜索地标文件来确定它,
就像它之前尝试处理 sys.executable 目录一样,直到找到一些东西。
如果没有,
sys.prefix
将留空。最后,完成所有这些之后,Python 加载
site
模块,该模块进一步向sys.path
添加内容:编辑:不再有
getpathp.c
< /a> (链接在复杂词的开头)自 2021 年 12 月,因为实现已移植到 Python:<代码>getpath.pyEDIT
When I wrote this back in 2015, there was no documentation on the subject. There is now per the comments, if you wanted to also check that out. There is also a prose explanation of the algorithm in the comments of
getpath.py
in the code base. I still believe my answer to be relevant and relatively current.ORIGINAL TEXT FOLLOWS
Python really tries hard to intelligently set
sys.path
. How it isset can get really complicated. The following guide is a watered-down,
somewhat-incomplete, somewhat-wrong, but hopefully-useful guide
for the rank-and-file python programmer of what happens when python
figures out what to use as the initial values of
sys.path
,sys.executable
,sys.exec_prefix
, andsys.prefix
on a normalpython installation.
First, python does its level best to figure out its actual physical
location on the filesystem based on what the operating system tells
it. If the OS just says "python" is running, it finds itself in $PATH.
It resolves any symbolic links. Once it has done this, the path of
the executable that it finds is used as the value for
sys.executable
, no ifs,ands, or buts.
Next, it determines the initial values for
sys.exec_prefix
andsys.prefix
.If there is a file called
pyvenv.cfg
in the same directory assys.executable
or one directory up, python looks at it. DifferentOSes do different things with this file.
One of the values in this config file that python looks for is
the configuration option
home = <DIRECTORY>
. Python will use this directory instead of the directory containingsys.executable
when it dynamically sets the initial value of
sys.prefix
later. If theapplocal = true
setting appears in thepyvenv.cfg
file on Windows, but not thehome = <DIRECTORY>
setting,then
sys.prefix
will be set to the directory containingsys.executable
.Next, the
PYTHONHOME
environment variable is examined. On Linux and Mac,sys.prefix
andsys.exec_prefix
are set to thePYTHONHOME
environment variable, ifit exists, superseding any
home = <DIRECTORY>
setting inpyvenv.cfg
. On Windows,sys.prefix
andsys.exec_prefix
is set to thePYTHONHOME
environment variable,if it exists, unless a
home = <DIRECTORY>
setting is present inpyvenv.cfg
,which is used instead.
Otherwise, these
sys.prefix
andsys.exec_prefix
are found by walking backwardsfrom the location of
sys.executable
, or thehome
directory given bypyvenv.cfg
if any.If the file
lib/python<version>/dyn-load
is found in that directoryor any of its parent directories, that directory is set to be to be
sys.exec_prefix
on Linux or Mac. If the filelib/python<version>/os.py
is is found in the directory or any of itssubdirectories, that directory is set to be
sys.prefix
on Linux,Mac, and Windows, with
sys.exec_prefix
set to the same value assys.prefix
on Windows. This entire step is skipped on Windows ifapplocal = true
is set. Either the directory ofsys.executable
isused or, if
home
is set inpyvenv.cfg
, that is used instead forthe initial value of
sys.prefix
.If it can't find these "landmark" files or
sys.prefix
hasn't beenfound yet, then python sets
sys.prefix
to a "fallback"value. Linux and Mac, for example, use pre-compiled defaults as the
values of
sys.prefix
andsys.exec_prefix
. Windows waitsuntil
sys.path
is fully figured out to set a fallback value forsys.prefix
.Then, (what you've all been waiting for,) python determines the initial values
that are to be contained in
sys.path
.sys.path
.On Windows, this is always the empty string, which tells python to
use the full path where the script is located instead.
sys.path
, unless you'reon Windows and
applocal
is set to true inpyvenv.cfg
.<prefix>/lib/python35.zip
on Linux/Mac andos.path.join(os.dirname(sys.executable), "python.zip")
on Windows, is added tosys.path
.applocal = true
was set inpyvenv.cfg
, then the contents of the subkeys of the registry keyHK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
are added, if any.applocal = true
was set inpyvenv.cfg
, andsys.prefix
could not be found,then the core contents of the of the registry key
HK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
is added, if it exists;applocal = true
was set inpyvenv.cfg
, then the contents of the subkeys of the registry keyHK_LOCAL_MACHINE\Software\Python\PythonCore\<DLLVersion>\PythonPath\
are added, if any.applocal = true
was set inpyvenv.cfg
, andsys.prefix
could not be found,then the core contents of the of the registry key
HK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
is added, if it exists;relative compile-time value of PYTHONPATH is added; otherwise, this step is ignored.
sys.prefix
.sys.exec_prefix
is added. On Windows, the directorywhich was used (or would have been used) to search dynamically for
sys.prefix
isadded.
At this stage on Windows, if no prefix was found, then python will try to
determine it by searching all the directories in
sys.path
for the landmark files,as it tried to do with the directory of
sys.executable
previously, until it finds something.If it doesn't,
sys.prefix
is left blank.Finally, after all this, Python loads the
site
module, which adds stuff yet further tosys.path
:EDIT: There is no more
getpathp.c
(link at the beginning on complicated word) since Dec 2021 because implementation was ported to Python:getpath.py
“从环境变量 PYTHONPATH 初始化,加上依赖于安装的默认值”
-- http: //docs.python.org/library/sys.html#sys.path
"Initialized from the environment variable PYTHONPATH, plus an installation-dependent default"
-- http://docs.python.org/library/sys.html#sys.path