Python 包管理工具 pipenv poetry
现代 Python 项目推荐使用 pipenv 或 poetry 管理依赖。
1. pipenv
安装
pip install pipenv
常用命令
# 进入项目目录
cd myproject
# 安装依赖
pipenv install
# 安装开发依赖
pipenv install --dev pytest
# 运行
pipenv run python main.py
# 进入虚拟环境
pipenv shell
Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
[dev-packages]
pytest = "*"
2. poetry
安装
curl -sSL https://install.python-poetry.org | python3 -
常用命令
# 初始化
poetry new myproject
# 添加依赖
poetry add requests
poetry add pytest --group dev
# 安装
poetry install
# 运行脚本
poetry run python main.py
# 进入 shell
poetry shell
pyproject.toml
[tool.poetry]
name = "myproject"
version = "0.1.0"
[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.28"
3. 对比
| 特性 | pipenv | poetry |
|---|---|---|
| 锁文件 | Pipfile.lock | poetry.lock |
| 默认pip | ✓ | ✗ |
| 构建 | setuptools | hatchling |
两者都比 pip + requirements.txt 更现代。