Skip to main content

Packges Manger

Packages Manager pip

Package source: https://pypi.org/

pip install <package-name>
pip install -U <package-name> # Upgrade
pip install <package-name>==2.28.1
pip show <package-name>

pip install -r requirement.txt

pip freeze
pip freeze | less # with pagination

Packages Manager Poetry

简介

Poetry 是一个现代化的 Python 依赖管理和打包工具,它结合了 pip、virtualenv、setuptools 和 pipenv 的功能。

Commands

poetry --version

# Create new project
## 交互式创建
poetry new my-project

## 或在现有项目中初始化
cd existing-project
poetry init

pyproject.toml 文件结构

[tool.poetry]
name = "my-project"
version = "0.1.0"
description = "My project description"
authors = ["Your Name <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.28.0"
numpy = "^1.24.0"

[tool.poetry.dev-dependencies]
pytest = "^7.0.0"
black = "^22.0.0"
flake8 = "^6.0.0"

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

依赖管理

# 生产依赖
poetry add requests pandas

# 开发依赖
poetry add --dev pytest black

# 指定版本
poetry add django@^4.0
poetry add "flask>=2.0,<3.0"


# 移除依赖
poetry remove requests
poetry remove --dev pytest

# 更新依赖
## 更新所有依赖
poetry update

## 更新特定包
poetry update requests

# 查看依赖
## 查看已安装依赖
poetry show

## 查看依赖树
poetry show --tree

## 查看过时依赖
poetry show --outdated

虚拟环境管理

# 创建虚拟环境
## Poetry 会自动创建虚拟环境
poetry install

# 虚拟环境配置
## 查看虚拟环境信息
poetry env info
## 列出所有虚拟环境
poetry env list
## 指定 Python 版本
poetry env use python3.9
## 删除虚拟环境
poetry env remove python3.9

# 配置虚拟环境路径
## 在 pyproject.toml 中配置
[tool.poetry]
# ... 其他配置

[tool.poetry.config]
virtualenvs.in-project = true # 在项目目录创建虚拟环境
virtualenvs.path = "~/.cache/pypoetry/virtualenvs" # 自定义路径

项目运行与脚本

# 在虚拟环境中运行
poetry run python script.py
poetry run pytest

# 在 pyproject.toml 中
[tool.poetry.scripts]
my-script = "my_package.module:main"
start = "python main.py"
test = "pytest tests/"

# 运行自定义脚本
poetry run my-script

构建与发布

# 构建项目
## 构建 wheel 和 sdist
poetry build
## 只构建 wheel
poetry build --format wheel

# 发布到 PyPI
## 配置 PyPI token
poetry config pypi-token.pypi your-token-here
## 发布
poetry publish
## 发布到测试 PyPI
poetry publish --repository testpypi

# 版本管理
## 显示当前版本
poetry version
## 设置新版本
poetry version patch # 0.1.0 -> 0.1.1
poetry version minor # 0.1.0 -> 0.2.0
poetry version major # 0.1.0 -> 1.0.0
poetry version 2.0.0 # 设置特定版本

配置管理

# 查看配置
poetry config --list

# 常用配置项
## 设置镜像源
poetry config repositories.aliyun https://mirrors.aliyun.com/pypi/simple/

## 启用并行安装
poetry config installer.max-workers 4

## 禁用虚拟环境
poetry config virtualenvs.create false

## 设置缓存时间
poetry config cache-dir /path/to/cache

插件系统

安装插件

poetry self add poetry-plugin-export

常用插件

  • poetry-plugin-export: 导出 requirements.txt
  • poetry-dynamic-versioning: 动态版本控制
  • poetry-multiproject-plugin: 多项目管理

常见问题

1. 清理缓存

poetry cache clear pypi --all

2. 解决依赖冲突

# 显示依赖冲突
poetry show --latest

# 尝试更新所有依赖
poetry update

3. 导出 requirements.txt

poetry export -f requirements.txt --output requirements.txt --without-hashes

4. 锁定文件管理

# 更新锁定文件
poetry lock --no-update

# 跳过锁定文件检查
poetry install --no-root

常用命令速查

命令说明
poetry new创建新项目
poetry init初始化现有项目
poetry add添加依赖
poetry remove移除依赖
poetry install安装所有依赖
poetry update更新依赖
poetry show显示依赖信息
poetry run在虚拟环境中运行命令
poetry shell进入虚拟环境 shell
poetry build构建包
poetry publish发布包
poetry version版本管理
poetry config配置管理
poetry env虚拟环境管理

History

  • 2026.01.28, Create by xiaoka, first version