[Python] How to resolve incompatible imports in python and pytest

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have created a project exactly as described by the pytest Good Integration Practices documentation, specifically the Tests outside application code section.

    This is my project layout

    .
    ├── pyproject.toml
    ├── src
    │ └── basic_package
    │ ├── bar.py
    │ ├── __init__.py
    │ └── main.py
    └── tests
    └── test_app.py



    In main.py I import from bar.py

    from bar import baz

    def main() -> str:
    return baz()


    in test_app.py

    from basic_package.main import main

    def test_foo():
    assert main() == 'qux'


    It works when I run the project. However, if I run pytest, I get an error


    ModuleNotFoundError: No module named 'bar'
    ​

    It works in pytest if I change the code in main.py to

    from .bar import baz


    But then if I run the application I get the ModuleNotFoundError

    How can I resolve this issue?

    I am running pytest from the root directory of the project

    Continue reading...

Compartilhe esta Página