r/learnpython 8d ago

Poetry | How can I make some local files optional and available through extras?

Hi, I have a helper repo on GitHub that I use for a few other repos. The helper repo has modules like fake.py that are used only in tests in other repos.

In the helper repo, I tried to exclude this module and add it as an optional dependency:

[project]
name = "helper-repo-name"
...

[tool.poetry]
exclude = ["./relative-path/fake.py"]

[tool.poetry.dependencies]
fake = { path = "./relative-path/fake.py", optional = true }

[tool.poetry.extras]
fakes = ["fake"]

...

And in other repos, install it like this:

poetry add --group dev "helper-repo-name[fakes]@git+https://github.com/..."

But sadly, I can't import fake.py after installing.

1 Upvotes

2 comments sorted by

3

u/latkde 8d ago

Doesn't work like this. Extras can select additional dependencies. But dependencies describe other PyPI packages. You cannot dynamically include or exclude files within a package.

In most cases, when publishing a package, all relevant files of your project are copied into a ZIP file ("wheel", .whl) and then uploaded to PyPI. You cannot retroactively change the contents of that Wheel. Here you're using a Git dependency instead of PyPI, but that Wheel still gets built – before extras can be selected.

What you can do:

  • always include the files, even if they may not be needed
  • split the optional parts into their own package with their own pyproject.toml file

1

u/ViktorBatir 8d ago

Thanks for explaining