This is my project structure (simplified):
my-project
├── __init__.py
├── my_code.py
└── tests
├── __init__.py
└── test_foo.py
When I run pytest ( with pytest .
from within my-project or pytest my-project
from its parent directory), python assumes the tests
directory as the root directory. However, I need python to identify my-project
as the root directory. How do I do that?
Background:
test_foo.py uses a relative import (from ..my_code import *
). If tests
directory is the root directory, python will not be able to resolve the import. For structural reasons(1) I cannot use absolute paths.
The relative import spawns the error ImportError: attempted relative import beyond top-level package
(1)
my-project
is maintained independently but is part (as a subfolder) in other projects. If the paths are absolute they would not be resolvable in the other projects. I use this structure over packaging, because packaging involves some management, that I need to avoid.