我遇到一些困難,瞭解本款在official tutorial:何時修改sys.path?
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
。我想用這個模塊來「遮蔽」標準模塊。
在終端,我執行並得到
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.argv'和'sys.path'。你在說哪一個? –
'sys'是*內置*模塊,不能被屏蔽。 –