Python: PYTHONHOME和PYTHONPATH的區別

Python: PYTHONHOME和PYTHONPATH的區別

 

 

目錄

PYTHONHOME和PYTHONPATH的區別

6.1.2. The Module Search Path

sys.path


 

 

PYTHONHOMEPYTHONPATH的區別

官方文檔解讀https://docs.python.org/3/tutorial/modules.html

6.1.2. The Module Search Path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.pathsys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).

  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • The installation-dependent default.

當導入名爲spam的模塊時,解釋器首先搜索具有該名稱的內置模塊。如果沒有找到,它會搜索一個名爲spam.py的文件。變量sys.path. sys給出的目錄列表中的py。路徑是從這些位置初始化的:

  • 包含輸入腳本的目錄(或未指定文件時的當前目錄)。
  • PYTHONPATH(一個目錄名列表,與shell變量PATH具有相同的語法)。
  • installation-dependent默認。

Note:On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search 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.

注意:在支持符號鏈接的文件系統上,包含輸入腳本的目錄是在符號鏈接之後計算的。換句話說,包含符號鏈接的目錄沒有添加到模塊搜索路徑中。初始化之後,Python程序可以修改sys.path。包含正在運行的腳本的目錄位於搜索路徑的開頭,在標準庫路徑的前面。這意味着將加載該目錄中的腳本,而不是庫目錄中的同名模塊。除非是有意替換,否則這是一個錯誤。有關更多信息,請參見標準模塊一節。

​​​​​​​

sys.path

sys.path 包含輸入模塊的目錄名列表,並且當前目錄也是sys.path的一部分。

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章