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']
Run Code Online (Sandbox Code Playgroud)
我的PYTHONPATH是:
PYTHONPATH=c:\testdir
Run Code Online (Sandbox Code Playgroud)
我想知道PYTHONPATH之前的那些路径来自哪里?
djh*_*987 68
Python真的很难智能地设置sys.path
.如何设置可能变得非常 复杂.下面的指南是一个打了折扣的,有点不完全,有些-错,但希望-有用的指南时Python会什么的使用会发生什么的职级和文件Python程序员的初始值的sys.path
,
sys.executable
,sys.exec_prefix
,和sys.prefix
在正常的
python安装上.
首先,python最好根据操作系统告诉它在文件系统上找出它的实际物理位置.如果操作系统只是说"python"正在运行,它会发现自己在$ PATH中.它解析了任何符号链接.完成此操作后,它找到的可执行文件的路径将用作值sys.executable
,no ifs,ands或buts.
接下来,确定用于初始值sys.exec_prefix
和
sys.prefix
.
如果pyvenv.cfg
在同一目录中调用了
一个文件sys.executable
或者一个目录,则python会查看它.不同的操作系统对此文件执行不同的操作.
python查找的此配置文件中的一个值是配置选项home = <DIRECTORY>
.Python将使用此目录而不是包含sys.executable
动态设置sys.prefix
稍后的初始值的目录.如果applocal = true
设置出现在pyvenv.cfg
Windows上的
文件中,而不是home = <DIRECTORY>
设置中,sys.prefix
则将设置为包含的目录sys.executable
.
接下来,PYTHONHOME
检查环境变量.在Linux和Mac上,
sys.prefix
并sys.exec_prefix
设置为PYTHONHOME
环境变量(如果存在),取代其中的任何home = <DIRECTORY>
设置pyvenv.cfg
.在Windows上,
sys.prefix
并sys.exec_prefix
设置为PYTHONHOME
环境变量(如果存在),除非存在home = <DIRECTORY>
设置pyvenv.cfg
,而是使用该设置.
否则,这些sys.prefix
并sys.exec_prefix
通过从位置倒着行走中发现sys.executable
,或home
通过指定目录pyvenv.cfg
(如有).
如果lib/python<version>/dyn-load
在该目录或其任何父目录中找到该文件,则该目录将设置为
sys.exec_prefix
在Linux或Mac上.如果该文件
lib/python<version>/os.py
是在目录或任何子目录中,该目录被设置为sys.prefix
在Linux,Mac和Windows,与sys.exec_prefix
设置为相同的值
sys.prefix
在Windows上.如果applocal = true
已设置,则在Windows上跳过整个步骤
.使用的目录,sys.executable
或者,如果home
设置pyvenv.cfg
,则使用的目录代替初始值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
设置为true pyvenv.cfg
.<prefix>/lib/python35.zip
Linux/Mac和os.path.join(os.dirname(sys.executable), "python.zip")
Windows 上
的zip文件路径sys.path
.applocal = true
设置pyvenv.cfg
,则HK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
添加注册表项子项的内容(
如果有).applocal = true
设置pyvenv.cfg
,并且sys.prefix
找不到,则添加注册表项的核心内容(HK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
如果存在);applocal = true
设置pyvenv.cfg
,则HK_LOCAL_MACHINE\Software\Python\PythonCore\<DLLVersion>\PythonPath\
添加注册表项子项的内容(
如果有).applocal = true
设置pyvenv.cfg
,并且sys.prefix
找不到,则添加注册表项的核心内容(HK_CURRENT_USER\Software\Python\PythonCore\<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并检查新添加的配置文件路径.
dfa*_*dfa 47
"从环境变量PYTHONPATH初始化,加上依赖于安装的默认值"
- http://docs.python.org/library/sys.html#sys.path
归档时间: |
|
查看次数: |
133944 次 |
最近记录: |