what’s wrong with my imports?
App folder structure:
myapp/
- models/models.py contains
SpotModel() - tests/tests.py contains TestSpotModel(unittest.TestCase). tests.py imports
from myapp.models.models import *which works like a charm - scripts/import.py contains
from myapp.models.models import *
the problem is that import.py when executed results in an error:
ImportError: No module named myapp.models.models
but tests.py runs.
I have __init__.py files in myapp/__init__.py, myapp/models/__init__.py, myapp/tests/__init__.py and as mentioned, running the unit tests using nosetests works as intended.
It is
__init__.pynotinit.py. Make sure each of the directory in hierarchy contains it in order to be able to import.EDIT: I managed to reproduce it.
Here’s the directory structure:
cesar@cesar-laptop:/tmp/asdasd$ tree . `-- myapp |-- __init__.py |-- models | |-- __init__.py | `-- models.py |-- scripts | |-- data.py | `-- __init__.py `-- tests |-- __init__.py `-- tests.pyI put the following code at the very beginning of the
data.pyto narrow down the problem:Running the
data.pythe way OP indicated yeilds ImportError:cesar@cesar-laptop:/tmp/asdasd$ python myapp/scripts/data.py ['/tmp/asdasd/myapp/scripts', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', -- Skipped -- '/usr/local/lib/python2.6/dist-packages'] Traceback (most recent call last): File "myapp/scripts/data.py", line 6, in from myapp.models.models import * ImportError: No module named myapp.models.modelsBut this way works like a charm:
Note the difference in the first entry of
sys.path.