Reputation: 521
I have a python 2.7 project which I have structured as below:
project
|
|____src
| |
| |__pkg
| |
| |__ __init__.py
|
|____test
|
|__test_pkg
| |
| |__ __init__.py
|
|__helpers
| |
| |__ __init__.py
|
|__ __init__.py
I am setting the src
folder to the PYTHONPATH
, so importing works nicely in the packages inside src
. I am using eclipse, pylint inside eclipse and nosetests in eclipse as well as via bash and in a make
file (for project). So I have to satisfy lets say every stakeholder!
The problem is importing some code from the helpers package in test. Weirdly enough, my test is also a python package with __init__.py
containing some top level setUp
and tearDown
method for all tests. So when I try this:
import helpers
from helpers.blaaa import Blaaa
in some module inside test_pkg
, all my stakeholders are not satisfied. I get the ImportError: No module named ...
and pylint also complains about not finding it. I can live with pylint complaining in test folders but nosetests
is also dying if I run it in the project directory and test directory. I would prefer not to do relative imports with dot (.).
Upvotes: 1
Views: 358
Reputation: 12322
The problem is that you can not escape the current directory by importing from ..helpers.
But if you start your test code inside the test directory with
python3 -m test_pkg.foo
the current directory will be the test directory and importing helpers will work. On the minus side that means you have to import from . inside test_pkg.
Upvotes: 1