vendredi 18 septembre 2020

relative imports in the test folder: ImportError

My Python package layout is usually like this:

.
├── setup.py
├── ...
│
├── foobar/
│   ├── __init__.py
│   ├── main.py
│   └── ...
│
└── test/
     ├── test_foo.py
     ├── test_bar.py
     ├── helpers.py
     └── ...

This works well. Now, when I try to add subfolders in test/,

.
└── test/
     ├── test_foo/
     │   └── test_feat0.py
     │
     ├── test_bar/
     │   └── test_feat1.py
     │
     └── helpers.py

I'm in trouble: The tests in test_feat0.py and test_feat1.py require something from helpers.py, but I cannot

from .. import helpers

because

E   ImportError: attempted relative import with no known parent package

I could of course just maintain two copies of helpers.py in test_foo/ and test_bar/ and import helpers, or go back to a flat structure, but that's not desirable.

How else can I work around relative imports in the test folder?

Aucun commentaire:

Enregistrer un commentaire