My typical Python project structure looks like
setup.py
foobar/
__init__.py
main.py
something_else.py
helpers.py
test/
addition/
test_consistency.py
test_speed.py
subtraction/
test_consistency.py
test_speed.py
This works well.
I then notice that the addition and subtraction tests share quite a bit of code, complex assertions and such. I tried to move the code into a helper file
test/
helpers.py
addition/
test_consistency.py
test_speed.py
subtraction/
test_consistency.py
test_speed.py
and import from addition/subtraction like
from .. import helpers
but this fails with
E ImportError: attempted relative import with no known parent package
Alright, the test folder is not a "package" and relative imports don't work. A well-known Python issue.
How can I share code between the addition and subtraction subfolders? A flat directory structure is not an option. (In real life, the folder structure is more complex than the above example.)
Weirdly, I noticed that pyscaffold does to relative imports in tests/
, see here. How do they do that?