Post-使用 Python 诗歌安装脚本

Post-install script with Python Poetry

Post-install script with Python setuptools

正是这个问题,但是有 Poetry 而没有 Setuptools。

我要运行

print('Installation finished, doing other things...')

安装我的软件包时。使用 Setuptools 你可以只修改 setup.py,但在 Poetry 中没有 setup.py.

我真正想做的是生成一个默认 .mypackage_config 文件并将其放在有用的地方。我不知道如何在没有任意代码的情况下执行此操作,但 Poetry does not allow arbitrary code 用于安装。有什么办法吗?

目前不可能(而且可能永远不会)

poetry 的整个理念是无需 运行 任何任意 Python 代码即可安装软件包。因此,自定义 post 安装脚本可能永远不会存在 (from the author of poetry, the link you gave in your question)。

你可以做什么

您可以使用 setuptools,并修改 setup.py 脚本,但通过删除对默认配置文件的需要,可能更容易删除对 post-install 脚本的需要。

大多数 Python 工具,例如 black,倾向于采用默认配置参数,除非配置文件(例如 pyproject.toml 文件)中有覆盖它们的设置, 例如:

[tool.black] # specifies this next section is the config for black
line-length = 88 # changes some configuration parameters from the defaults
target-version = ['py36', 'py37']

Jupyter 有一个命令 jupyterhub --generate-config 来生成默认配置文件 (source)。

如果您的包是一个库,而不是命令行工具,即您通过 import-ing 在您的 Python 代码中使用它,我建议您只传入配置作为 constructor/functions 的参数,因为这是将配置传递给库的标准方式。

例如,请看PySerial's API。您可以通过将 args 传递给构造函数 serial.Serial 来配置库,例如:

import serial
with serial.Serial(
    '/dev/ttyUSB0', # first config param, the port
    19200, # second config param, the baudrate
    timeout=5, # sets the timeout config param
) as ser:

在我当前的项目中,我们决定创建一个 bootstrap 脚本,也许这是最好的方法