poetry: `ModuleNotFoundError` using `from` in `packages`
-
Poetry version:
1.7.1
. -
Python version:
3.11.6
. -
OS version and name: macOS 14.1.2.
-
I am on the latest stable Poetry version, installed using a recommended method.
-
I have searched the issues of this repo and believe that this is not a duplicate.
-
I have consulted the FAQ and blog for any relevant entries or release notes.
-
If an exception occurs when executing a command, I executed it again in debug mode (
-vvv
option) and have included the output below.
Issue
I’ll admit, right off the bat, that I’m not sure what the expected behaviour here is or what exactly is causing my issues. This might not even be a poetry
problem. So apologies up front if I’m off.
Regardless, here’s what I see.
I have a project that I’m migrating from a flat structure to a src
layout. In its current state, it looks like this:
my-project/
├── project/
│ ├── __init__.py
│ ├── package/
│ │ ├── __init__.py
│ │ └── ...
│ └── module.py
├── tests/
│ ├── __init__.py
│ └─── package/
│ ├── __init__.py
│ └── test_package.py
├── plugins/
│ ├── __init__.py
│ └── commitizen.py
├── pyproject.toml
└── README.md
# pyproject.toml
[tool.poetry]
name = "my-project"
version = "0.1.0"
packages = [{ include = "project" }]
[tool.poetry.plugins."commitizen.plugin"]
# References plugins top level package
cz_jira_conventional = "plugins.commitizen:JiraConventionalCz"
This installs fine and allows me to execute binaries such as:
poetry run cz --help
poetry run pytest .
# etc.
However, after changing my layout to use src/
and updating pyproject.toml
packages
accordingly, those commands above stop working, raising errors related to missing modules.
my-project/
├── src/
│ └── project/
│ ├── __init__.py
│ ├── package/
│ │ ├── __init__.py
│ │ └── ...
│ └── module.py
├── tests/
│ ├── __init__.py
│ └── package/
│ ├── __init__.py
│ └── test_package.py
├── plugins/
│ ├── __init__.py
│ └── commitizen.py
├── pyproject.toml
└── README.md
Note that only sources under project
moved locations. Everything else, including plugins/
and tests/
stayed the same.
# pyproject.toml
[tool.poetry]
name = "my-project"
version = "0.1.0"
packages = [{ include = "project", from = "src" }]
poetry install
runs successfully, but then results in:
poetry run cz --help
Traceback (most recent call last):
# ...
ModuleNotFoundError: No module named 'plugins'
poetry run pytest .
tests/test_package.py:5: in <module>
from tests.factories import (
E ModuleNotFoundError: No module named 'tests'
I found two ways to work around this:
- Append
PYTHONPATH=.
to allpoetry run
commands (from project’s root). - Add
{ include = "plugins" }, { include = "tests" }
to thepackages
inpyproject.toml
(and re-runpoetry install
).
But none of them seem like things I should really be doing or caring about — I certainly didn’t need to before.
Could you help me understand what exactly is going on? Am I misusing something?
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 22 (12 by maintainers)
Seems like you have lots of solutions
include
I don’t think it’s going to be worthwhile to put more effort into the - let’s say unconventional - mixed layout
Please close