带有 Python Poetry 的安装后脚本

Isa*_*ner 5 python python-poetry

使用 Python setuptools 的安装后脚本

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

我想跑

print('Installation finished, doing other things...')
Run Code Online (Sandbox Code Playgroud)

当我的包安装时。使用 Setuptools,您可以只进行修改setup.py,但在 Poetry 中特别没有setup.py

我真正想做的是生成一个默认.mypackage_config文件并将其放在有用的地方。我不知道如何在没有任意代码的情况下执行此操作,但是 Poetry不允许安装任意代码。有没有办法做到这一点?

Alo*_*ink 12

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

诗歌的整个想法是,可以在不运行任何任意Python代码的情况下安装包。因此,自定义安装后脚本可能永远不会存在(来自诗歌的作者,您在问题中给出的链接)。

你可以做什么

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

大多数 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']
Run Code Online (Sandbox Code Playgroud)

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

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

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

[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']
Run Code Online (Sandbox Code Playgroud)