使用Poetry管理Python项目

Poetry 命令

Poetry version 1.1.11

# 从requirements.txt导入依赖
cat requirements.txt | grep -E '^[^# ]' | cut -d= -f1 | xargs -n 1 poetry add

# 导出依赖到requirements.txt (带hash)
poetry export --output requirements.txt
# 导出依赖到requirements.txt (不带hash)
poetry export --without-hashes -f requirements.txt --output requirements.txt

# 删除虚拟环境
poetry env remove python3

# 安装所有依赖
poetry install
# 仅安装非 development 环境的依赖,一般部署时使用
poetry install --no-dev

# 安装依赖
poetry add <pkg> [--dev]

# 移除依赖
poetry remove <pkg>

# 更新所有锁定版本的依赖
poetry update
# 更新指定的依赖
poetry update <pkg>

# 激活虚拟环境
poetry shell
# 退出虚拟环境
exit

# 创建新项目
poetry new <project_name>

# 在已有项目中使用 poetry
poetry init

# 追踪&更新包
poetry show
# 添加--tree 参数选项可以查看依赖关系
poetry show --tree
# 查看可以更新的依赖
poetry show --outdated

poetry config

配置一些 poetry 的默认行为

  1. 设置虚拟环境默认安装到项目的 .venv 目录里:

    poetry config virtualenvs.in-project true
    
  2. 部署时先使用以下命令可以使所有的包安装到系统中,而不是虚拟环境里

    poetry config virtualenvs.create false --local
    

使用Pypi镜像

通过在 pyproject.toml 中配置源可以使 poetry 从指定的 PyPi 镜像中拉取代码。注意 default = true 是必须的,否则 poetry 仍然会从默认源拉取哈希值。

[[tool.poetry.source]]
name = "tsinghua"
default = true
url = "https://pypi.tuna.tsinghua.edu.cn/simple"

poetry本地调试包

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