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).

把配置文件与下载脚本加入版本管理和持续集成进行版本管理即可。

你可能感兴趣的:(编程语言)