ImportError: No module named 'superlists.lists'

So... starting to work my way thru the book 'Test Driven Development with Python' and I'm trying (against the author's recommendations) to do it using an IDE, namely PyCharm.

I got the first functional test working (using selenium and unittest), but when I got to the part for tests.py I started having problems.

My directory structure looks like this:

superlists/
├── db.sqlite3
├── functional_tests.py
├── __init__.py
├── lists
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
├── superlists
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── templates

When I click 'Tools > Run manage.py Task > test' (and select 'All applications') the test fails.  At this point in the text, it is actually supposed to fail, just not like this:

Code
Error
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 574, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: superlists.lists.tests
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'superlists.lists'
prettyPrint();

Maybe I'm missing something elementary... but it seems pretty obvious that there is a module 'superlists.lists', as created by PyCharm itself.  Using Python 3.4.0 on Ubuntu 14.04, in a virtualenv, with just pip, setuptools, django 1.7 and selenium installed in the virtualenv.

Any ideas/hints?

Thanks,

Monte
4 comments
Comment actions Permalink
You have two directories named superlists, and it looks like the top one is part of your sys.path.  If you move lists inside the inner superlists folder it should work (or import it as just `lists`).
0
Comment actions Permalink
Actually... I did some more tinkering, including closing out PyCharm and opening up ye ole terminal app.  Activated the virtualenv from the command line, and ran 'python manage.py task' again - same results.

Then I recreated the entire project again from the command line using django-admin.py, side-by-side with a slightly different name (supperlist) and tried again - and got the results I expected i.e. the test inside tests.py ran (failed, as they were supposed to at this point, but they ran).

I listed both directories in separate windows using the 'tree' command in each... and the only significant difference was that there was an extra __init__.py in the top level folder 'superlist' (the one created by pycharm) that was not there in 'supperlist' (the one created manually using django-admin.py.  The only thing in the file was one line:

__author__ = 'monte'
prettyPrint();

I renamed that file temporarily and re-ran 'python manage.py test' - and it worked as it should.

Not sure why that __init__.py in that location would screw up loading tests.py from inside /lists...?
0
Comment actions Permalink
This is funny. I'm reading also this book and stumbled into the same problem. But slightly a bit different...

.
├── functional_tests.py
└── superlists
    ├── db.sqlite3
    ├── lists
    │   ├── __init__.py
    │   ├── admin.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── templates
    │   │   └── home.html
    │   ├── tests.py
    │   └── views.py
    ├── manage.py
    └── superlists
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py



If I run now within PyCharm manage.py test I get this error:
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py test lists.tests.HomePageTest.test_home_page_returns_correct_html /Users/fp/PycharmProjects/TDD/superlists
Testing started at 18:37 ...
There is no such manage file manage

There is no such settings file superlists.settings
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 129, in <module>
    utility.execute()
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 104, in execute
    PycharmTestCommand().run_from_argv(self.argv)
  File "/Users/fp/Library/Python/3.4/lib/python/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/fp/Library/Python/3.4/lib/python/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/fp/Library/Python/3.4/lib/python/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/fp/Library/Python/3.4/lib/python/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 91, in handle
    failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive, failfast=failfast, keepdb='--keepdb' in sys.argv)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 232, in run_tests
    extra_tests=extra_tests, **options)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 132, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
  File "/Users/fp/Library/Python/3.4/lib/python/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 96, in build_suite
    suite = super(DjangoTeamcityTestRunner, self).build_suite(*args, **kwargs)
  File "/Users/fp/Library/Python/3.4/lib/python/site-packages/django/test/runner.py", line 66, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 105, in loadTestsFromName
    module = __import__('.'.join(parts_copy))
ImportError: No module named 'lists'
prettyPrint();

If I run this straight from the shell without (!!!) changing the code the tests passes:
$ python3 manage.py test
Creating test database for alias 'default'...
..
----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK
Destroying test database for alias 'default'...
prettyPrint();

Looks like PyCharm is messing up the Django folders?

EDIT:
Just found several bugs in the bug tracker linked to this issue. Will probably be fixed in build 140.792
0
Comment actions Permalink
You're a hero, thanks!
0

Please sign in to leave a comment.