Skip to content

pytest or python -m pytest

Currently, pytest is launched via _python -m pytest which is different as running pytestdirectly.

Here is my context.

I have two python modules in two separated projects: lib1 and lib2. In this project we use a common namespace, so the import are:

import project.lib1
import project.lib2

As it is an old project, the layout is simple:

lib1.git
├── setup.py
├── ...
├── project/
│   ├── __init__.py
│   └── lib1/
│       ├── __init__.py
│       ├── ...
│       └── module1.py
└── tests/
    ├── ...
    └── test_module1.py
lib2.git
├── setup.py # requirement for lib1
├── ...
├── project/
│   ├── __init__.py
│   └── lib2/
│       ├── __init__.py
│       ├── ...
│       └── module1.py
└── tests/
    ├── ...
    └── test_module1.py # import project.lib1

And finally, lib1 is a requirement for lib2.

When using _python -m pytest the import of project.lib1 fails because the module is not found.

When using pytest directly, it works.

The reason (I understood): the python -m module prepend the current working directory in the Python's path as documented in https://docs.python.org/3/library/sys.html#sys.path

Proposition

Replace _python -m pytest by _run pytest.

Edited by Guilhem Bonnefille