使用 Poetry 从 Git 自动 post 安装 Python 依赖项

Automating post-install of Python dependency from Git using Poetry

我正在为 ASN.1 使用 Poetry for Python dependency management, along with PyCrate encoding/decoding。

PyCrate 是从 GitHub 中提取的依赖项,一旦从 GitHub 中提取,就可以通过 运行 在 PyCrate 目录中安装一个安装文件来安装。

python setup.py install

如果可能的话,我想将安装步骤整合到我的 pyproject.toml 中。我目前的 pyproject.toml 包括 PyCrate 如下:

…
[tool.poetry.dependencies]
pycrate = {git = "https://github.com/P1sec/pycrate.git"}
…

这将从 GitHub 存储库中提取 PyCrate,但会放入由 Poetry 创建的 virtualenv 中的 src 文件夹中。

有什么方法可以在执行 poetry install 时自动 运行 安装脚本?我已经研究过使用 Poetry scripts,但到目前为止还无法正确使用 运行ning。

我当前的设置涉及 运行 宁 poetry install,然后手动 运行宁 setup.py install 用于 PyCrate,但是我想要我的 poetry install 执行完整设置,如果可以的话。

如有任何帮助,我们将不胜感激。

当你运行poetry install.

时,诗歌应该已经运行python setup.py install给你了

Poetry 基本上只是 运行s pip install package,它下载包,基本上只是 运行s python setup.py install 在包上!

Under the hood, [pip] will run python setup.py install

来源:

但是,poetry 仅在隔离的虚拟环境中安装包,以避免污染计算机的其余部分。

要运行有诗意的东西,你需要运行用poetry run YOUR_COMMAND

为了运行虚拟环境里面的脚本,你必须运行poetry shell进入虚拟环境,否则poetry run YOUR_COMMAND。例如。 运行 一个 Python 脚本,你应该做 poetry run python your_python_script.py

例子

如果您有一个包含以下 pyproject.toml 文件的文件夹:

[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.6"
pycrate = {git = "https://github.com/P1sec/pycrate.git"}

[tool.poetry.dev-dependencies]

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

在运行ning poetry install之后,你可以通过运行ning poetry run SCRIPT_NAME访问所有pyrcrate脚本:

# works because pycrate_showmedia.py was installed with poetry install
me@computer:~/example-project$ poetry run poetry run pycrate_showmedia.py
usage: pycrate_showmedia.py [-h] [-bl BL] [-wt] input
pycrate_showmedia.py: error: the following arguments are required: input

如果你有一个导入 pycrate 库的 Python 文件,它也需要 运行 使用 poetry run:

me@computer:~/example-project$ cat test.py 
import pycrate_core
print(pycrate_core.__version__)
me@computer:~/example-project$ poetry run python test.py