sys.path是什么时候修改的?
When is sys.path modified?
我在理解 official tutorial 中的这一段时遇到一些困难:
After initialization, Python programs can modify sys.path
. The
directory containing the script being run is placed at the beginning
of the search path, ahead of the standard library path. This means
that scripts in that directory will be loaded instead of modules of
the same name in the library directory. This is an error unless the
replacement is intended. See section Standard Modules for more
information.
说,我有以下模块,名为 demo.py
:
if __name__ == '__main__':
import sys
print sys.path
当前目录下还有一个名为sys.py
的模块,只包含一个pass
。我想将此模块用于 "shadow" 标准模块。
在终端,我执行得到了
sunqingyaos-MacBook-Air:Documents sunqingyao$ python demo.py
['/Users/sunqingyao/Documents', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
所以我的问题是:sys.path
是什么时候修改的?
- 如果在之前
import sys
被执行,sys.py
应该导入而不是标准模块。
- 如果在
print sys.path
执行后被修改,'/Users/sunqingyao/Documents'
不应该出现在sys.path
.
而且奇怪的是,修改发生在 import sys
和 print sys.path
之间。
sys
是一个内置模块,它是解释器的一部分,不能被屏蔽,因为它在解释器启动时已经加载。
那是因为 sys.modules
是加载模块的核心注册表,而 sys.modules['sys']
指向它自己。在需要搜索模块路径之前,任何 import sys
语句都会找到 sys.modules['sys']
。
sys
不是唯一的内置模块,尽管它是唯一一个自动加载的模块。有关编译到 Python 二进制文件中的其他模块,请参阅 sys.builtin_module_names
tuple。
这是site
module to update sys.path
; it is loaded as part of the Python bootstrap process, unless you used the -S
command line switch的责任。
我在理解 official tutorial 中的这一段时遇到一些困难:
After initialization, Python programs can modify
sys.path
. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.
说,我有以下模块,名为 demo.py
:
if __name__ == '__main__':
import sys
print sys.path
当前目录下还有一个名为sys.py
的模块,只包含一个pass
。我想将此模块用于 "shadow" 标准模块。
在终端,我执行得到了
sunqingyaos-MacBook-Air:Documents sunqingyao$ python demo.py
['/Users/sunqingyao/Documents', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
所以我的问题是:sys.path
是什么时候修改的?
- 如果在之前
import sys
被执行,sys.py
应该导入而不是标准模块。 - 如果在
print sys.path
执行后被修改,'/Users/sunqingyao/Documents'
不应该出现在sys.path
.
而且奇怪的是,修改发生在 import sys
和 print sys.path
之间。
sys
是一个内置模块,它是解释器的一部分,不能被屏蔽,因为它在解释器启动时已经加载。
那是因为 sys.modules
是加载模块的核心注册表,而 sys.modules['sys']
指向它自己。在需要搜索模块路径之前,任何 import sys
语句都会找到 sys.modules['sys']
。
sys
不是唯一的内置模块,尽管它是唯一一个自动加载的模块。有关编译到 Python 二进制文件中的其他模块,请参阅 sys.builtin_module_names
tuple。
这是site
module to update sys.path
; it is loaded as part of the Python bootstrap process, unless you used the -S
command line switch的责任。