Tom Johnson
Tom Johnson

Reputation: 2164

Convincing poetry to exclude files from the build

I'm using poetry to build my package. I configure pyproject.toml to include all the files in tests but want to exclude tests\not_this_dir. The goal is that as I add additional stuff under tests they are picked up automatically. Because I have some tests that use proprietary data sets, I put those in tests\not_this_dir so they are not distributed.

The issue I'm having is that I can't convince poetry to exclude not_this_dir. Here's the content of my abbreviated pyproject.toml.

[tool.poetry]
name = "mypkg"
version = "0.2.0"
include = [
    { path = "data" },
    { path = "tests" },

exclude = [
    { path = "tests/not_this_dir" }
]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

The file structure is:

mypkg
¦   .gitignore
¦   pyproject.toml
¦   README.txt
¦       
+---mypkg
¦   ¦   a.py
¦   ¦   b.py
¦   ¦   __init__.py
¦           
+---data
¦       e.json
¦       f.json
¦       
+---tests
¦   ¦   conftest.py
¦   ¦   test_g.py
¦   ¦   test_h.py
¦   ¦   __init__.py
¦   ¦               
¦   +---data
¦   ¦   +---g
¦   ¦   ¦   ¦   otherfile.txt
¦   ¦   ¦               
¦   ¦   +---h
¦   ¦       ¦   differentfile.txt
¦   +---not_this_dir
¦   ¦   +---g
¦   ¦   ¦   ¦   otherfile2.txt
¦   ¦   ¦               
¦   ¦   +---h
¦   ¦       ¦   differentfile2.txt

When I run poetry build it's including not_this_dir. The files in not_this_dir are in git, so I don't want to add them to .gitignore. The only thing that seems to work is to completely give up on the use of exclude and configure pyproject.toml like this:

[tool.poetry]
name = "mypkg"
version = "0.2.0"
include = [
    { path = "data" },
    { path = "tests/*.py" },
    { path = "tests/data" },

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

so that it only explicitly includes the files I want, rather than explicitly excluding them. I've tried various exclude glob variants such as tests/**/not_this_dir, tests/not_this_dir/* and nothing seems to work. The poetry documentation at https://python-poetry.org/docs/1.1/pyproject/ is super vague on the interaction between include and exclude and what the allowed file glob syntax is. It seems like include overrides exclude, or exclude is just being ignored?

Upvotes: 4

Views: 7359

Answers (1)

Tom Johnson
Tom Johnson

Reputation: 2164

I finally got a tip from this issue https://github.com/python-poetry/poetry/issues/1597. What will make this work is to NOT use the path = syntax in the exclude section. You need to change

exclude = [
    { path = "tests/not_this_dir" }
]

to

exclude = [
    "tests/not_this_dir"
]

Then it will exclude not_this_dir as expected.

Upvotes: 8

Related Questions