My project is not recognizing the src directory / package for my project
- Poetry version: Poetry (version 1.7.1)
- Python version: Python: 3.11.7
- OS version and name: MacOS Ventura 13.3
- pyproject.toml:
- [x] I am on the latest stable Poetry version, installed using a recommended method.
- [x] I have searched the issues of this repo and believe that this is not a duplicate.
- [x] 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 (
-vvvoption) and have included the output below.
Issue
I had a similar issue as this link https://github.com/python-poetry/poetry/issues/8477 My problem is that I have my code files contained in src/my_api and my tests at the same level as the src directory under test
project structure looks like
project
|_src
|_ my_api
|_ __init__.py
|_ main.py
|_ test
|_ __init__.py
|_ test.py
The issue im running into is that when I run my app (simple web server), it will not recognize the packages if they are declared as
src.my_api.constants
but it will recognize them as
my_api.constants
The error I get for this is ModuleNotFoundError: No module named 'src'
on the other hand my tests will not recognize the files at all unless they are declared as
src.my_api.constants
so I can only run either my tests or my app. the tests are unit tests using the python unittest library and pytest integration tests. Both are in the same test package.
Ive looked at the previous issues and am not sure what to do to resolve this as it seems they are at odds with each other.
when I use pip list I can see that the src package and the my_api package are both listed so im not clear why they are inaccessible.
im using a simple web server using this package from http.server import BaseHTTPRequestHandler, HTTPServer
Im working on a python virtual environment started with python -m venv myvenv
and I ran poetry install as soon as I activated it
This certainly works fine - it is how poetry itself is arranged.
Perhaps you forgot to poetry install.
I just created a new virtual env and did a fresh poetry install but im still getting the same ModuleNotFoundError: No module named 'src'
That is what should happen. There is no module named src.
cause there is no init file right? But then when I run my unit tests they require src to be in the package name in the imports in those files. So under src, I would have to do from src.my_api.constants import *. Its weird that its both
when I run my unit tests they require src to be in the package name in the imports
no they do not. Again, poetry itself is a project that demonstrates that what you are trying to do works fine.
You either have not installed the project, or have not activated the virtual environment with the installed project when you run the tests, or have done something that I cannot guess.
In all cases, you should assume that there is no poetry bug.
Ok. I'll have to look elsewhere for the issue then. Thanks for your help.
@pkalaga1 I had a similar directory structure to you, this worked for me and I didn't even need to include my package name:
packages = [ { include = "src/" }, { include = "src/**/*.py" }, ]
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.