如何从由 pyproject.toml 管理的库的 wheel 中排除 “tests” 文件夹?

huangapple go评论54阅读模式
英文:

how to exclude "tests" folder from the wheel of a pyproject.toml managed lib?

问题

我尽力从一个由 `setup.py` 管理的库迁移到一个纯粹使用 `pyproject.toml` 的库。
我有以下文件夹结构:

tests
└── <文件>
docs
└── <文件>
sepal_ui
└── <文件>
pyproject.toml


在我的 `pyproject.toml` 中,我对文件和包的发现设置如下:
```toml
[build-system]
requires = ["setuptools>=61.2", "wheel"]

[tool.setuptools]
include-package-data = false

[tool.setuptools.packages.find]
include = ["sepal_ui*"]
exclude = ["docs*", "tests*"]

在生成的 wheel 中,我得到以下内容:

tests
└── <文件>
docs
└── <文件>
sepal_ui
└── <文件>
sepal_ui.egg-info
└── top-level.txt

查看 top-level.txt,我发现只有 sepal_ui 被包含了,所以我的问题很简单:为什么额外的 "docs" 和 "tests" 文件夹仍然被包含,即使它们没有被使用?如何摆脱它们?

附注:我知道 MANIFEST.in 解决方案,如果这真的是唯一的解决方案,我将接受,但我觉得在两个文件中指定是多余的。


<details>
<summary>英文:</summary>

I try my best to move from a `setup.py` managed lib to a pure `pyproject.toml` one. 
I have the following folder structure:

tests
└── <files>
docs
└── <files>
sepal_ui
└── <files>
pyproject.toml


and in my `pyproject.toml` the following setup for file and packages discovery:
```toml
[build-system]
requires = [&quot;setuptools&gt;=61.2&quot;, &quot;wheel&quot;]

[tool.setuptools]
include-package-data = false

[tool.setuptools.packages.find]
include = [&quot;sepal_ui*&quot;]
exclude = [&quot;docs*&quot;, &quot;tests*&quot;]

and in the produce wheel, I get the following:

tests
└── &lt;files&gt;
docs
└── &lt;files&gt;
sepal_ui
└── &lt;files&gt;
sepal_ui.egg-info
└── top-level.txt

looking at the top-level.txt, I see that only sepal_ui is included so my question is simple why do the extra "docs" and "tests" folder are still included even if they are not used? how to get rid of them ?

PS: I'm aware of the MANIFEST.in solution that I will accept if it's really the only one but I found it redundant to specify in 2 files.

答案1

得分: 9

Fun fact, it was working from the start....

Small debugging workflow for the next person that does not want to spend hours for nothing.

configuration of the pyproject.toml

the following configuration is the minimal to remove files from a docs/ and tests/ folders that are at the root of the repository. If you disseminated your tests in each modules, consider adding *.tests*:

[build-system]
requires = ["setuptools>=61.2", "wheel"]

[tool.setuptools]
include-package-data = false

[tool.setuptools.packages.find]
include = ["sepal_ui*"]
exclude = ["docs*", "tests*"]

Clean environment

That was my mistake. Python is cahing information in the build/ and egg-info folder. setuptools will use the lest of files stored in egg-info/SOURCE.txt so first step: get rid of previous build files.

Then simply run:

python -m build

check both wheel and tar.gz

From the start I was checking only tar.gz thinking (naively) that .whl and .tar.gz were the same. It's not while the tests folder remains in the tar.gz file, it's absent from the wheel.

英文:

Fun fact, it was working from the start....

Small debugging workflow for the next person that does not want to spend hours for nothing.

configuration of the pyproject.toml

the following configuration is the minimal to remove files from a docs/ and tests/ folders that are at the root of the repository. If you disseminated your tests in each modules, consider adding *.tests*:

[build-system]
requires = [&quot;setuptools&gt;=61.2&quot;, &quot;wheel&quot;]

[tool.setuptools]
include-package-data = false

[tool.setuptools.packages.find]
include = [&quot;sepal_ui*&quot;]
exclude = [&quot;docs*&quot;, &quot;tests*&quot;]

Clean environment

That was my mistake. Python is cahing information in the build/ and egg-info folder. setuptools will use the lest of files stored in egg-info/SOURCE.txt so first step: get rid of previous build files.

Then simply run:

python -m build

check both wheel and tar.gz

From the start I was checking only tar.gz thinking (naively) that .whl and .tar.gz were the same. It's not while the tests folder remains in the tar.gz file, it's absent from the wheel.

答案2

得分: 0

这对刚孵化出来的项目对我有用:

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build]
exclude = [
  ".*",
  "docs",
  "tests",
]
英文:

This has worked for me for hatchling:

[build-system]
requires = [&quot;hatchling&quot;]
build-backend = &quot;hatchling.build&quot;

[tool.hatch.build]
exclude = [
  &quot;/.*&quot;,
  &quot;/docs&quot;,
  &quot;/tests&quot;,
]

huangapple
  • 本文由 发表于 2023年2月8日 23:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定