python打包和依赖管理工具poetry

作者: dreamfly 分类: python 发布时间: 2023-03-30 17:03

poetry

Poetry 是 Python 中用于依赖管理和打包的工具。 它允许您声明项目所依赖的库,并将为您管理(安装/更新)它们。 Poetry 提供了一个锁文件来确保可重复安装,并且可以构建您的项目以进行分发。

系统要求

poetry需要Python 3.7+。它是多平台的,可以出色地工作 在 Linux、macOS 和 Windows 上。

安装

linux,mac,wsl安装方式

curl -sSL https://install.python-poetry.org | python3 -

windows 安装方式(powershell)

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -

使用

poetry --version
poetry self update

zsh 配置自动补全

poetry completions zsh > ~/.zfunc/_poetry

修改.zshrc 添加路径

fpath+=~/.zfunc
autoload -Uz compinit && compinit

0h-my-zsh 配置自动补全

mkdir $ZSH_CUSTOM/plugins/poetry
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry
plugins(
    poetry
    ...
    )

创建项目

poetry new poetry-demo

初始化存在的项目

cd pre-existing-project
poetry init

添加依赖

poetry add pendulum

根据配置文件安装依赖

poetry install

发布python 包

poetry publish

显示目录信息

$ poetry show --tree
requests-toolbelt 0.8.0 A utility belt for advanced users...
└── requests <3.0.0,>=2.0.1
    ├── certifi >=2017.4.17
    ├── chardet >=3.0.2,<3.1.0
    ├── idna >=2.5,<2.7
    └── urllib3 <1.23,>=1.21.1

$ poetry show --latest
pendulum 2.0.4   1.4.5 Python datetimes made easy.
django   1.11.11 2.0.3 A high-level Python Web framework ...
requests 2.18.4  2.18.4 Python HTTP for Humans.

管理环境

poetry env use 3.7

查看环境

poetry env info
poetry env list

删除环境

poetry env remove /full/path/to/python
poetry env remove python3.7
poetry env remove 3.7
poetry env remove test-O3eWbxRl-py3.7

依赖于git

[tool.poetry.dependencies]
# Get the latest revision on the branch named "next"
requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" }
# Get a revision by its commit hash
flask = { git = "https://github.com/pallets/flask.git", rev = "38eb5d3b" }
# Get a revision by its tag
numpy = { git = "https://github.com/numpy/numpy.git", tag = "v0.13.2" }

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!