如何将 PEP 517 和 PEP 518 用于本地可用(不在 PYPI 上)、系统范围的 CLI Python 脚本

How to use PEP 517 and PEP 518 for locally available (not on PYPI), system-wide CLI Python scripts

我正在将我的工作扩展到 CLI 应用程序中以获得一些自动化和其他生活质量的好处,并开始为这些项目使用 setup.cfg 和 pyproject.toml 而不是通常的 requirements.txt。问题是我真的不希望这些应用程序暂时不要在 PyPi 上共享,虽然我正在研究基本的机制和用例,但喜欢使用这些工具连接是多么容易 python打包到我的 OS 的可执行路径。通常,我会使用 pipx 来帮助我解决这个问题并直接从源安装,但我很好奇您是否有其他策略。

pyproject.toml python 包装中的入口点摘要

所以我终于想通了这个野兽并知道如何初始化入口点脚本并使其易于作为可执行文件添加到系统的 PATH——基本上,我们可以使用 Python 打包 conventions/automation 来创建system-wide/user-wide 任何命令行实用程序的可访问性。

在 Python 包装 API 中,有 入口点 我们可以指定,它们在过去的 PEP 中被设定并通过 setup.py。 Python 中的包装正在经历缓慢的演变,最终将从 setup.pysetup.cfg 迁移永久地 pyproject.toml (example of what this new format will look in PEP 621) and the toml language specification, which is being unofficially endorsed by the Python Software Foundation as the preferred file format over yaml (read more about why).

在上面的超链接“示例”中,我们看到了这一点:(了解有关 differences 的更多信息) [project.scripts] spam-cli = “垃圾邮件:main_cli”

[project.gui-scripts]
spam-gui = "spam:main_gui"

[project.entry-points."spam.magical"]
tomatoes = "spam:main_tomatoes"

这些替换 setup() 对象中的以下选项 entry_points

# source: https://the-hitchhikers-guide-to-packaging.readthedocs.io/en/latest/creation.html#entry-points

setup(name='zest.releaser',
      ...
      entry_points={
          'console_scripts':
              ['release = zest.releaser.release:main',
               'prerelease = zest.releaser.prerelease:main',
               ]}
      )

其中令人兴奋的部分是我们可能会(最终)将整个打包问题减少到仅 pyproject.toml 文件(我们还没有做到这一点,请参阅有关使用 setup.py 作为垫片的信息,因此我们仍然可以从 pip install --editable . 命令中受益,该命令具有更好的可读性的额外好处,locking down build environment and package environment dependencies, and creating a universal standard API interface through which various third party dependency tools like Poetry (extra source) and build tools like Black (and it's CLI config options) 可以集中他们的配置,以创建更 pythonic、确定性的方式来创建新软件和现有软件的有效构建和打包。