Python的模塊搜索路徑和包管理

Python模塊搜索路徑

Python模塊搜索路徑的官方文檔:
https://docs.python.org/3/tutorial/modules.html#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.path. sys.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).

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.

除了上述路徑,還可以通過 .pth文件來配置搜索路徑,此文件放到python可搜索到的位置即可,建議site-packages下:
https://docs.python.org/3/library/site.html

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped.

如果出現模塊找不到,可以通過 sys.path 來看搜索路徑:

Python 3xx
>>> import sys
>>> sys.path

修改搜索路徑可以通過:

  • 可以在代碼裏動態改這個變量,然後之後執行import;
  • 可以在當前的shell導出環境變量PYTHONPATH;
  • 可以在site-packages添加一個路徑文件x.pth文件;

但這些都是全局的,如果想做到隔離的包管理,用venv機制。

用venv做隔離的包管理

從Python 3.5+後,官方鬼推薦venv,venv解決了同一臺機器上多個python項目依賴不同的第三方庫的問題。官方說明:
https://docs.python.org/3/library/venv.html

創建方法如下

# for Linux/macOS
python3 -m venv /path/to/new/virtual/environment

針對windows系統,命令也是類似,只不過路徑是windows風格了,建議是用PyCharm可以自動配置。

使用 venv 創建後,進入虛擬環境:

# Linux/macOS 下對於 bash/zsh
# 進入venv,activate就是個shell腳本,導出下環境變量
$ source <venv>/bin/activate
# 退出venv,deactive就是個shell函數,source後被直接命令調用了
$ deactive 

舉例:

➜  ~ pwd
/Users/michael
➜  ~ python3 -m venv kitty
➜  ~ source kitty/bin/activate
(kitty) ➜  ~ echo $PATH
/Users/michael/kitty/bin:/Users/michael/bin:/usr/local/bin
(kitty) ➜  ~ which python3
/Users/michael/kitty/bin/python3
(kitty) ➜  ~ deactivate
➜  ~ echo $PATH
/Users/michael/bin:/usr/local/bin
➜  ~ which python3
/usr/local/bin/python3

下面是官方的具體說明:

You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path.
You can deactivate a virtual environment by typing “deactivate” in your shell. The exact mechanism is platform-specific and is an internal implementation detail (typically a script or shell function will be used).

把配置文件與下載腳本加入版本管理和持續集成進行版本管理即可。

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