Pycharm fails to find package in the defined PYTHONPATH
I have a Python monorepo. One of the services in this monorepo is "poller-service". Its general structure is:
services/
└── poller-service/
├── .venv/
| └─ ...
├── main.py
├── src/
| └─── mycompany/
| ├── __init__.py
| └── poller/
| ├── __init__.py
| └── api/
| ├── __init__.py
| └── app.py
├── pyproject.tomlmain.py is:
import asyncio
from mycompany.poller.api.app import app
async def main():
...
if __name__ == '__main__':
asyncio.run(main())As you can see, main.py imports variable from app.py, which lies under the package "mycompany.poller.api" in "/src" sub-folder.
When I launch the service from Pycharm, I use the Python interpreter defined in .venv sub-folder. This is how the launcher looks:

When I run the launcher I get the error:
/Users/me/repositories/services/poller-service/.venv/bin/python /Users/me/repositories/services/poller-service/main.py -m univorn --port 8006 --reload
Traceback (most recent call last):
File "/Users/me/repositories/Droxi-services/Python/droxi-poller/main.py", line 12, in <module>
from mycompany.poller.api.app import app # noqa: E402
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'mycompany.poller'I tried to check whether /src subfolder is included in PYTHONPATH by running the lines inside main.py:
import sys
import pprint
pprint.pprint(sys.path)and I definitely see that /src sub-folder is included in PYTHONPATH. If so, why do I get this error?
The weirdest part is that when I run the command that the launcher executes under the hood:
/Users/me/repositories/services/poller-service/.venv/bin/python /Users/me/repositories/services/poller-service/main.py -m univorn --port 8006 --reload
I don't get this error.
This is probably a problem within Pycharm. Do you know why it happens?
请先登录再写评论。
Hi Matan1b1l,
Just to confirm - if the
/srcdirectory is marked as sources root?Even though you've added
/srctoPYTHONPATH, PyCharm's script-based run configuration sets the working directory to/poller-service, and then runsmain.pydirectly as a script, which alters how module resolution works in Python. This behavior affects how relative imports are resolved.On the other hand, when you run the same command manually from the terminal, Python likely handles the module structure correctly because the
__main__context is different.Thanks a lot for your comment.
Yes. “src” directory is marked as sources root. I also tries to mark the “poller-service” as source root. Both options showed the same error.
I understand that when I run Python application from Pycharm, the PYTHONPATH will not be the same PYTHONPATH like I run it from command line.
If so, what is the solution? How can I run the application by Pycharm launcher and make “src” folder a part of PYTHONPATH or make it recognized during module resolution?
Matan1b1l,
You can verify this by printing
sys.pathfrom within the PyCharm run configuration and comparing it to the output from your system terminal.Have you tried to set “Working directory” to
/services/poller-service/src. Does it help?If adjusting the working directory doesn’t resolve the issue, please consider creating a ticket on our issue tracker: https://youtrack.jetbrains.com/issues/PY.
When submitting the report, please include:
changing working directory to /services/poller-service/src didn't help :-(
What do you mean “printing
sys.pathfrom within the PyCharm run configuration”?I added the line print(sys.path) to main.py and ran the launcher.
This line printed a list of libraries, and services/droxi-poller/src was one of them. However, I still got the error.
Anyway, I opened an issue:
https://youtrack.jetbrains.com/issue/PY-82693/Pycharm-fails-to-find-package-in-the-defined-PYTHONPATH