Yesterday, I was recommended to have a look at Shopyo - Open inventory management and Point of sales.

As I am passionate about testing and CI, I always have a look at configuration files for newly discovered projects.

After I cloned the repository, I tried to run the tests.

So, without having a look into the documentation ( :-/ ), I

  • created a virtual env
  • installed the dependencies
  • ran pytest from the root of the project
  • … and got a ModuleNotFoundError

The friendly maintainers of the projects pointed me in the right direction on how to run tests for this project:

cd shopyo
python -m pytest

And indeed, it worked.

The project does not follow the src “convention”, so I roughly knew what was going on, but I missed the exact reason.

Florian Bruhin gave me the answer on Twitter:

Does it work with PYTHONPATH=. pytest? The main difference is that python -m adds the current directory to sys.path, i.e. lets you import modules from there.

So, that’s it - python -m pytest adds the current directory to sys.path! This makes a lot of sense!

Later, Paul Ganssle linked to his excellent blog post “Testing your python package as installed”, where he explained this aspect and the whole to src or not to src story in detail.