SeanJarp
SeanJarp

Reputation: 129

'no module' error with __init__.py in directory using Nosetests

I have the following directory structure:

Chippa/

      bin/
         __init__.py
         app.py

      tests/
         __init__.py
         app_tests.py
         tools.py

      templates/
         hello_form.html
         index.html
         layout.html

      docs/

In my app_tests.py file, I have:

from nose.tools import *
from bin.app import app
from tests.tools import assert_response

When I try to run app_tests.py from outside of the tests directory, one level above the test directory inside the Chippa directory, like so:

python tests/app_tests.py

I get the following error:

Traceback (most recent call last):
  File "tests/app_tests.py", line 3, in <module>
    from bin.app import app
ImportError: No module named bin.app

But I do have an empty __init__.py in the bin directory, which I thought would have prevented this issue. What am I missing here?

Upvotes: 1

Views: 161

Answers (1)

Ry-
Ry-

Reputation: 225125

For that import to work, you have to actually be running a proper module in the first place. So, rather than

python tests/app_tests.py

, try

python -m tests.app_tests

Upvotes: 2

Related Questions