Miniconda 和 poetry 搭建 Python 开发环境(支持多版本、依赖管理)

基于自己的日常习惯测试整理,通过 Windows 系统演示(Linux 系统操作步骤大同小异)。Miniconda 用来提供 conda 命令管理多个 Python 版本(如 Python 3.8、Python 3.9);poetry 则用来创建基于项目的虚拟环境,维护对应的包依赖关系。

一、效果演示

conda 命令查看安装的 Python 版本:

C:\Users\Administrator>conda env list
# conda environments:
#
base                  *  C:\Users\xniu\Miniconda3
python2.7.18             C:\Users\xniu\Miniconda3\envs\python2.7.18
python3.9.4              C:\Users\xniu\Miniconda3\envs\python3.9.4
conda env list

poetry 查看某个项目的包依赖关系:

(python3.9.4) C:\Users\Administrator\projects\auto-test>poetry show
certifi  2020.12.5 Python package for providing Mozilla's CA Bundle.
chardet  4.0.0     Universal encoding detector for Python 2 and 3
idna     2.10      Internationalized Domain Names in Applications (IDNA)
requests 2.25.1    Python HTTP for Humans.
selenium 3.141.0   Python bindings for Selenium
urllib3  1.26.4    HTTP library with thread-safe connection pooling, file post, and more.

(python3.9.4) C:\Users\Administrator\projects\auto-test>poetry show -t
requests 2.25.1 Python HTTP for Humans.
|-- certifi >=2017.4.17
|-- chardet >=3.0.2,<5
|-- idna >=2.5,<3
`-- urllib3 >=1.21.1,<1.27
selenium 3.141.0 Python bindings for Selenium
`-- urllib3 *
poetry show

二、安装 Miniconda

Miniconda 软件提供了 conda 命令,可以用来创建基于不同 Python 版本的虚拟环境。

访问 Miniconda 官网,下载对应系统版本的安装包并安装。

package

安装完成后,添加 conda 命令的路径(安装目录下的 Scripts 目录)到 PATH 环境变量。其路径一般为 C:\Users\xxx\Miniconda3\Scripts\

添加完成后,打开一个新的命令提示符,运行 conda 命令看是否有反应。

创建基于不同 Python 版本的虚拟环境

conda create -n python3.9.4 python=3.9.4
上述命令会创建一个新的 Python 虚拟环境,并安装 Python 3.9.4。

其中 -n 选项用于指定该虚拟环境的名称,方便后续通过 conda activate xxx 启用该虚拟环境;
python=3.9.4 命令用于安装指定版本的 Python 程序。

虚拟环境创建成功后,即可使用 conda activate python3.9.4 命令启用该虚拟环境。
此后在该命令提示符环境下任何 python 命令都会自动使用 Python3.9.4 执行。

conda env list 命令可以查看现有的 Python 虚拟环境。

PS:更多 conda 命令可参考 Command reference。

三、安装 poetry

poetry 的安装可参考 poetry 官方文档。对于 Windows 系统可直接打开一个 PowerShell 窗口,运行以下命令:
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

安装成功后,poetry 命令会自动添加到 PATH 环境变量中。

创建基于项目的虚拟环境

打开一个新的命令提示符,使用 conda activate python3.9.4 命令激活某个 Python 版本。

进入到项目路径下,运行 poetry init 命令初始化配置。

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry init

This command will guide you through creating your pyproject.toml config.

Package name [test-poetry]:
Version [0.1.0]:
Description []:
Author [None, n to skip]:  n
License []:
Compatible Python versions [^3.9]:

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file

[tool.poetry]
name = "test-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name "]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"


Do you confirm generation? (yes/no) [yes] yes

上述操作会在项目目录下自动创建 pyproject.toml 配置文件,内容如下:

[tool.poetry]
name = "test-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name "]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

使用 poetry shell 命令自动创建基于当前项目的虚拟环境并激活该环境:

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry shell
Creating virtualenv test-poetry-thSlgjIV-py3.9 in C:\Users\Administrator\AppData\Local\pypoetry\Cache\virtualenvs
Spawning shell within C:\Users\Administrator\AppData\Local\pypoetry\Cache\virtualenvs\test-poetry-thSlgjIV-py3.9
Microsoft Windows [Version 10.0.18363.1316]
(c) 2019 Microsoft Corporation. All rights reserved.

运行 code . 命令使用 VSCode 软件打开本项目,此时即可在 IDE 中切换到新创建的基于本项目的虚拟环境(VSCode 已经安装了 Python 插件)。

VSCode

安装依赖包

poetry add xxx 命令可以用来在当前环境中安装某个依赖包:

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry add requests
Using version ^2.25.1 for requests

Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 5 installs, 0 updates, 0 removals

  • Installing certifi (2020.12.5)
  • Installing chardet (4.0.0)
  • Installing idna (2.10)
  • Installing urllib3 (1.26.4)
  • Installing requests (2.25.1)

poetry add xxx -D 命令可以用来安装针对开发环境的某个依赖包(用 poetry remove 命令卸载此类包时也需要指定 -D 选项):

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry add autopep8 -D
Using version ^1.5.7 for autopep8

Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 3 installs, 0 updates, 0 removals

  • Installing pycodestyle (2.7.0)
  • Installing toml (0.10.2)
  • Installing autopep8 (1.5.7)

同时,安装的依赖包信息也会自动添加到 pyproject.toml 配置文件中:

[tool.poetry]
name = "test-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name "]

[tool.poetry.dependencies]
python = "^3.9"
requests = "^2.25.1"

[tool.poetry.dev-dependencies]
autopep8 = "^1.5.7"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

后续配置新的环境时,pyproject.toml 可以发挥类似 requirements.txt 文件的作用。即借助此文件中的配置,可以直接使用 peotry install 命令自动安装文件中包含的依赖项。

维护环境依赖

使用 poetry show 命令查看当前安装的依赖包列表:

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry show
autopep8    1.5.7     A tool that automatically formats Python code to conform to the PEP 8 style guide
certifi     2020.12.5 Python package for providing Mozilla's CA Bundle.
chardet     4.0.0     Universal encoding detector for Python 2 and 3
idna        2.10      Internationalized Domain Names in Applications (IDNA)
pycodestyle 2.7.0     Python style guide checker
requests    2.25.1    Python HTTP for Humans.
toml        0.10.2    Python Library for Tom's Obvious, Minimal Language
urllib3     1.26.4    HTTP library with thread-safe connection pooling, file post, and more.

使用 poetry show -t 命令查看当前环境中各包之间的依赖关系:

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry show -t
autopep8 1.5.7 A tool that automatically formats Python code to conform to the PEP 8 style guide
|-- pycodestyle >=2.7.0
`-- toml *
requests 2.25.1 Python HTTP for Humans.
|-- certifi >=2017.4.17
|-- chardet >=3.0.2,<5
|-- idna >=2.5,<3
`-- urllib3 >=1.21.1,<1.27

若此时使用 poetry remove autopep8 -D 命令移除 autopep8,则之前自动安装的 pycodestyletoml 依赖项也会被移除。

(python3.9.4) C:\Users\Administrator\projects\python\test-poetry>poetry remove autopep8 -D
Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 0 installs, 0 updates, 3 removals

  • Removing autopep8 (1.5.7)
  • Removing pycodestyle (2.7.0)
  • Removing toml (0.10.2)

PS:更多 poetry 命令和用法可参考官方文档:Poetry Commands

你可能感兴趣的:(Miniconda 和 poetry 搭建 Python 开发环境(支持多版本、依赖管理))