Python的sys.path从哪里初始化?
Python的sys.path从哪里初始化?
UPD :Python在引用PYTHONPATH之前添加了一些路径:
>>> import sys
>>> from pprint import pprint as p
>>> p(sys.path)
['',
'C:Python25libsite-packagessetuptools-0.6c9-py2.5.egg',
'C:Python25libsite-packagesorbited-0.7.8-py2.5.egg',
'C:Python25libsite-packagesmorbid-0.8.6.1-py2.5.egg',
'C:Python25libsite-packagesdemjson-1.4-py2.5.egg',
'C:Python25libsite-packagesstomper-0.2.2-py2.5.egg',
'C:Python25libsite-packagesuuid-1.30-py2.5.egg',
'C:Python25libsite-packagesstompservice-0.1.0-py2.5.egg',
'C:Python25libsite-packagescherrypy-3.0.1-py2.5.egg',
'C:Python25libsite-packagespyorbited-0.2.2-py2.5.egg',
'C:Python25libsite-packagesflup-1.0.1-py2.5.egg',
'C:Python25libsite-packageswsgilog-0.1-py2.5.egg',
'c:testdir',
'C:Windowssystem32python25.zip',
'C:Python25DLLs',
'C:Python25lib',
'C:Python25libplat-win',
'C:Python25liblib-tk',
'C:Python25',
'C:Python25libsite-packages',
'C:Python25libsite-packagesPIL',
'C:Python25libsite-packageswin32',
'C:Python25libsite-packageswin32lib',
'C:Python25libsite-packagesPythonwin']
我的PYTHONPATH是:
PYTHONPATH=c:testdir
我想知道PYTHONPATH之前的路线是从哪里来的?
“从环境变量PYTHONPATH初始化,加上依赖于安装的默认值”
- http://docs.python.org/library/sys.html#sys.path
Python真的很难智能地设置sys.path
。 它如何设置会变得非常复杂。 下面的指南是一个贬低的,有点不完整的,有点不对的,但有用的指南,用于排名和文件的python程序员,当python找出什么用作sys.path
的初始值时会发生什么, sys.executable
, sys.exec_prefix
和sys.prefix
在一个正常的python安装。
首先,python根据操作系统告诉它的最佳级别来确定它在文件系统上的实际物理位置。 如果操作系统只是说“python”正在运行,它会发现自己在$ PATH中。 它解决了任何符号链接。 完成此操作后,它找到的可执行文件的路径将用作sys.executable
可执行文件的值,而不是ifs, sys.executable
或buts。
接下来,它确定sys.exec_prefix
和sys.prefix
的初始值。
如果有一个名为pyvenv.cfg
在同一目录sys.executable
或一级目录,蟒蛇看着它。 不同的操作系统对这个文件做了不同的事情。
python查找的配置文件中的一个值是配置选项home = <DIRECTORY>
。 Python将在sys.executable
动态设置sys.prefix
的初始值时使用此目录而不是包含sys.executable
的目录。 如果pyvenv.cfg
applocal = true
设置出现在Windows上的pyvenv.cfg
文件中,但不是home = <DIRECTORY>
设置,则sys.prefix
将设置为包含sys.executable
的目录。
接下来,检查PYTHONHOME
环境变量。 在Linux和Mac上, sys.prefix
和sys.exec_prefix
被设置为PYTHONHOME
环境变量(如果存在),取代pyvenv.cfg
任何home = <DIRECTORY>
设置。 在Windows中, sys.prefix
和sys.exec_prefix
设置为PYTHONHOME
环境变量,如果它存在,除非一个home = <DIRECTORY>
设置为存在于pyvenv.cfg
,其被替代使用。
否则,这些sys.prefix
和sys.exec_prefix
可以从sys.executable
的位置向后走,或者是pyvenv.cfg
给出的home
目录(如果有的话)。
如果在该目录或其任何父目录中找到文件lib/python<version>/dyn-load
,则该目录将设置为Linux或Mac上的sys.exec_prefix
。 如果文件lib/python<version>/os.py
是在目录或任何子目录中,该目录被设置为sys.prefix
在Linux,Mac和Windows,与sys.exec_prefix
设置为相同的值作为Windows上的sys.prefix
。 如果设置了applocal = true
则在Windows上跳过整个步骤。 任一目录sys.executable
被使用,或者如果home
中设置pyvenv.cfg
,用来代替的初始值sys.prefix
。
如果它找不到这些“标志性”文件或sys.prefix
尚未找到,则python sys.prefix
设置为“fallback”值。 例如,Linux和Mac使用预编译的默认值作为sys.prefix
和sys.exec_prefix
的值。 Windows会等待,直到sys.path
完全解决为sys.prefix
设置回退值。
然后,(你一直在等待什么)python决定了sys.path
包含的初始值。
sys.path
。 在Windows上,这总是空字符串,它告诉python改为使用当前工作目录。 sys.path
,除非你使用的是Windows和applocal
设置为true在pyvenv.cfg
。 <prefix>/lib/python35.zip
和<prefix>/lib/python35.zip
上的os.path.join(os.dirname(sys.executable), "python.zip")
的zip文件路径被添加到sys.path
。 pyvenv.cfg
没有设置pyvenv.cfg
applocal = true
,则会添加注册表项HK_CURRENT_USERSoftwarePythonPythonCore<DLLVersion>PythonPath
的子项的内容(如果有的话)。 pyvenv.cfg
没有设置pyvenv.cfg
applocal = true
,并且sys.prefix
,则会添加注册表项HK_CURRENT_USERSoftwarePythonPythonCore<DLLVersion>PythonPath
的核心内容,如果存在的话; pyvenv.cfg
没有设置pyvenv.cfg
applocal = true
,则会添加注册表项HK_LOCAL_MACHINESoftwarePythonPythonCore<DLLVersion>PythonPath
的子项的内容(如果有的话)。 pyvenv.cfg
没有设置pyvenv.cfg
applocal = true
,并且sys.prefix
,则会添加注册表项HK_CURRENT_USERSoftwarePythonPythonCore<DLLVersion>PythonPath
的核心内容,如果存在的话; sys.prefix
被添加。 sys.exec_prefix
的值。 在Windows上,添加了用于(或将用于)动态搜索sys.prefix
。 在Windows上的这个阶段,如果没有找到前缀,那么python将尝试通过搜索sys.path
中的所有目录来确定它的地标文件,因为它试图先前处理sys.executable
目录,直到找到一些东西。 如果没有, sys.prefix
留空。
最后,在所有这些之后,Python加载了site
模块,它进一步向sys.path
添加了一些东西:
它首先从头部和尾部构建四个目录。 对于头部分,它使用sys.prefix和sys.exec_prefix; 空头被跳过。 对于尾部,它使用空字符串,然后使用lib / site-packages(在Windows上)或lib / pythonX.Y / site-packages,然后使用lib / site-python(在Unix和Macintosh上)。 对于每个不同的头尾组合,它会查看它是否指向现有目录,如果是,则将其添加到sys.path中,并检查新添加的配置文件路径。
链接地址: http://www.djcxy.com/p/55283.html上一篇: Where is Python's sys.path initialized from?
下一篇: wsgi and virtual env