WestCoastProjects
WestCoastProjects

Reputation: 63172

How can pip/poetry find the python modules under a non-root directory like 'src'?

I have structured our python project in a typical manner:

project root
|--src
    |--my_pkg
          -- my_module.py
          --__init__.py
    |--tests

I have followed poetry docs for include/exclude

enter image description here

Based on that I added the following to pyproject.toml:

[tool.poetry]
..
include = ["src/*"]

But i get

 ValueError

  my_pkg is not a package.

How can I tell poetry to look under src for the my_pkg.my_module ?

Upvotes: 2

Views: 2994

Answers (2)

z0r
z0r

Reputation: 8585

I'm building an AWS Lambda function, so my source directory is pretty bare-bones:

src
├── foo.py
└── bar.py

I've found that I need this in my pyproject.toml:

[tool.poetry]
name = "baz"
packages = [ { include = "*.py", from = "src" } ]

Note: no mention of the package name in the packages entry.

Upvotes: 1

finswimmer
finswimmer

Reputation: 15202

Don't put a __init__.py in the src folder. There's no need for it.

If you have this in your pyproject.toml

[tool.poetry]
name = "my-pkg"

Poetry will find my_pkg in the src automatically. But you can be more explicit by:

[tool.poetry]
# ...
packages = [
    { include = "my_pkg", from = "src" },
]

See https://python-poetry.org/docs/pyproject/#packages

Upvotes: 2

Related Questions