[Python] python series of No module named <module name>

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    Consider this project tree:

    project
    │
    └── app/
    │
    ├── main.py
    ├── __init__.py
    │
    ├── common/
    │ ├── __init__.py
    │ └── do_common.py
    │
    └── util/
    ├── __init__.py
    └── do_util.py


    main.py

    import sys
    from pathlib import Path

    from common.do_common import foo

    sys.path.append(str(Path(__file__).resolve().parent))

    foo()


    do_common.py

    def foo():
    print("foo")


    run with activated venv

    python -m app.main


    I get an error ModuleNotFoundError: No module named 'common'

    To fix this, I would need to change the import to

    from .common.do_common import foo #### Adding a dot 「.」before `common` module


    and able to print foo

    However, if I do call the do_util.py from do_common.py like below:

    from util.do_util import bar


    def foo():
    print("foo")
    bar()


    I get ModuleNotFoundError: No module named 'util'

    Changing the import to

    from .util.do_util import bar


    returns

    ModuleNotFoundError: No module named 'app.common.util'


    How should I fix the issue where I wont need to prepend the modules with a dot?

    It is Python 3.12 in windows 10.

    Continue reading...

Compartilhe esta Página