python import error
Question: 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.


Answer: It is `__init__.py` not `init.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.py
    

I put the following code at the very beginning of the `data.py` to narrow down
the problem:

    
    
    import sys
    import pprint
    
    pprint.pprint(sys.path)
    
    from myapp.models.models import *
    

Running the `data.py` the 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.models
    

But this way works like a charm:

    
    
    cesar@cesar-laptop:/tmp/asdasd$ python -m myapp.scripts.data
    ['',
     '/usr/lib/python2.6',
     '/usr/lib/python2.6/plat-linux2',
     '/usr/lib/python2.6/lib-tk',
     -- Skipped --
    '/usr/local/lib/python2.6/dist-packages']
    

Note the difference in the first entry of `sys.path`.

