[Python] Pycharm fails to find package in the defined PYTHONPATH

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    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.toml



    main.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:

    [​IMG]

    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?

    Continue reading...

Compartilhe esta Página