使用 pyproject.toml 和 setuptools 从包数据中包含或排除(许可证)文件

Sil*_*ron 5 python setuptools python-wheel python-packaging pyproject.toml

长话短说

\n

如何可靠地将来自LICENSES/(重用样式)的文件包含在源存档中,并为具有src/布局的 Python 包提供轮子?如何排除特定文件?

\n

细节

\n

我的项目结构看起来像

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LICENSES\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MAIN.txt\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 SECUNDARY.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MANIFEST.in\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 random_package\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo1.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo2.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 submodule1\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar1.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 submodule2\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar2.cpp\n
Run Code Online (Sandbox Code Playgroud)\n

看起来pyproject.toml

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LICENSES\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MAIN.txt\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 SECUNDARY.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MANIFEST.in\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 random_package\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo1.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo2.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 submodule1\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar1.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 submodule2\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar2.cpp\n
Run Code Online (Sandbox Code Playgroud)\n

如何包含除安装之外的所有 cpp 文件? submodule1/bar1.cpp

\n

我已经在 toml 中尝试了以下条目(一次一个):

\n
[build-system]\nrequires = ["setuptools>=61.0"]\nbuild-backend = "setuptools.build_meta"\n\n[project]\nname = "random_package"\nversion = "0.1.0"\nlicense = {file = "LICENSES/MAIN.txt"}\n\n[metadata]                          # EDIT: metadata was the issue\nlicense-files = ["LICENSES/*.txt"]  # this line should be in [tool.setuptools]\n\n[tool.setuptools]\npackage-dir = {"" = "."}\ninclude-package-data = true  # tried both true and false\n\n[tool.setuptools.packages.find]\nwhere = ["."]\ninclude = ["random_package*"]\n
Run Code Online (Sandbox Code Playgroud)\n

我什至设置include-package-data为 false 并手动输入 cpp 文件(bar1.cpp 除外),甚至这对源和轮子都不起作用。

\n

没有什么可以可靠地工作:对于这些选项的任何和所有组合,我总是在 zip/tar.gz 存档或轮子中获得 bar1.cpp python -m build

\n

至于许可证文件,我进入LICENSE/MAIN.txt了源构建,但没有进入其他版本,并且轮子中不存在许可证。

\n

部分解决方案

\n

我有一些适用于源 dist 的东西,使用MANIFEST.in包含LICENSES/*.txt文件和手动包含 .cpp 文件而不是数据选项,pyproject.toml但即使这也不适用于轮子:我没有获得许可证random_package-0.1.0.dist-info

\n

我对轮子中的许可证文件的期望是错误的吗?使用旧setup.py方案,当我使用单个License.txt文件时,我确实在那里获得了许可证文件......并且没有办法单独使用 toml 来做到这一点吗?

\n

Sil*_*ron 5

事实证明,我弄错了位置(我第一次在文档的“元数据”部分license-files看到它);它实际上必须在. 其他数据包含问题可能是缓存问题,它似乎在以下情况下工作:[tool.setuptools]pyproject.toml

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "random_package"
license = {file = "LICENSES/MAIN.txt"}
version = "0.1.0"

[tool.setuptools]
package-dir = {"" = "."}
include-package-data = false
license-files = ["LICENSES/*.txt"]

[tool.setuptools.packages.find]
where = ["."]
include = ["random_package*"]

[tool.setuptools.package-data]
random_package = ["*.cpp"]

[tool.setuptools.exclude-package-data]
"*" = ["bar1.cpp"]
Run Code Online (Sandbox Code Playgroud)

这样,MANIFEST.in就不需要任何文件了。