说服诗歌从构建中排除文件

Tom*_*son 3 python-packaging python-poetry

我正在用诗歌来构建我的包。我配置 pyproject.toml 以包含所有文件,tests但想要排除tests\\not_this_dir. 目标是当我在下面添加其他内容时,tests它们会被自动拾取。因为我有一些使用专有数据集的测试,所以我将它们放入其中,tests\\not_this_dir这样它们就不会被分发。

\n

我遇到的问题是我无法说服诗歌排除not_this_dir. 这是我的缩写的内容pyproject.toml

\n
[tool.poetry]\nname = "mypkg"\nversion = "0.2.0"\ninclude = [\n    { path = "data" },\n    { path = "tests" },\n\nexclude = [\n    { path = "tests/not_this_dir" }\n]\n\n[build-system]\nrequires = ["poetry-core"]\nbuild-backend = "poetry.core.masonry.api"\n
Run Code Online (Sandbox Code Playgroud)\n

文件结构为:

\n
mypkg\n\xc2\xa6   .gitignore\n\xc2\xa6   pyproject.toml\n\xc2\xa6   README.txt\n\xc2\xa6       \n+---mypkg\n\xc2\xa6   \xc2\xa6   a.py\n\xc2\xa6   \xc2\xa6   b.py\n\xc2\xa6   \xc2\xa6   __init__.py\n\xc2\xa6           \n+---data\n\xc2\xa6       e.json\n\xc2\xa6       f.json\n\xc2\xa6       \n+---tests\n\xc2\xa6   \xc2\xa6   conftest.py\n\xc2\xa6   \xc2\xa6   test_g.py\n\xc2\xa6   \xc2\xa6   test_h.py\n\xc2\xa6   \xc2\xa6   __init__.py\n\xc2\xa6   \xc2\xa6               \n\xc2\xa6   +---data\n\xc2\xa6   \xc2\xa6   +---g\n\xc2\xa6   \xc2\xa6   \xc2\xa6   \xc2\xa6   otherfile.txt\n\xc2\xa6   \xc2\xa6   \xc2\xa6               \n\xc2\xa6   \xc2\xa6   +---h\n\xc2\xa6   \xc2\xa6       \xc2\xa6   differentfile.txt\n\xc2\xa6   +---not_this_dir\n\xc2\xa6   \xc2\xa6   +---g\n\xc2\xa6   \xc2\xa6   \xc2\xa6   \xc2\xa6   otherfile2.txt\n\xc2\xa6   \xc2\xa6   \xc2\xa6               \n\xc2\xa6   \xc2\xa6   +---h\n\xc2\xa6   \xc2\xa6       \xc2\xa6   differentfile2.txt\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行时poetry build它包括not_this_dir. 中的文件not_this_dir位于 git 中,所以我不想将它们添加到.gitignore. 似乎唯一有效的方法是完全放弃使用exclude和配置,pyproject.toml如下所示:

\n
[tool.poetry]\nname = "mypkg"\nversion = "0.2.0"\ninclude = [\n    { path = "data" },\n    { path = "tests/*.py" },\n    { path = "tests/data" },\n\n[build-system]\nrequires = ["poetry-core"]\nbuild-backend = "poetry.core.masonry.api"\n
Run Code Online (Sandbox Code Playgroud)\n

这样它只显式包含我想要的文件,而不是显式排除它们。我尝试过各种排除 glob 变体,例如tests/**/not_this_dirtests/not_this_dir/*但似乎没有任何效果。https://python-poetry.org/docs/1.1/pyproject/上的诗歌文档对于包含和排除之间的交互以及允许的文件 glob 语法是什么非常模糊。似乎包含覆盖了排除,或者排除只是被忽略了?

\n

Tom*_*son 6

我终于从这个问题中得到了提示https://github.com/python-poetry/poetry/issues/1597。使这项工作有效的是不使用path = 排除部分中的语法。你需要改变

exclude = [
    { path = "tests/not_this_dir" }
]
Run Code Online (Sandbox Code Playgroud)

exclude = [
    "tests/not_this_dir"
]
Run Code Online (Sandbox Code Playgroud)

然后它将not_this_dir按预期排除。