HELP on unittest.loader.ModuleImportFailure

2,233 views
Skip to first unread message

Kim

unread,
Jun 7, 2014, 9:00:39 AM6/7/14
to django...@googlegroups.com
Hi,

I started learning Django and am using "Test Driven Web Development with Python" book. 

On page 21, the book mentions to test using lists/tests.py.
The book uses the code:
$ python3 manage.py test
to run the test. 

However, the manage.py file sits in the upper superlists folder and the tests.py sits in lists folder. 
I keep getting an error message saying "ImportError: No module named lists.tests”. 

Could someone help me figure this out? 

My Django project directory looks like below. 

superlists
  db.sqlite3
  functional_test.py
  manage.py
  lists
    __init__.py
    admin.py
    models.py
    tests.py
    views.py
  superlists
    __init__.py
    settings.py
    urls.py 
    wsgi.py
__init__.py

The error message is below:

$ python manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: superlists.lists.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: superlists.lists.tests
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
ImportError: No module named lists.tests


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
Destroying test database for alias 'default'...

Kelvin Wong

unread,
Jun 8, 2014, 6:33:32 PM6/8/14
to django...@googlegroups.com
You need to be in the first superlists directory. You have a db created so you must have run syncdb at some point. Go back to that folder:

$ pwd
/home/you/superlists

$ ls
__init__.py   db.sqlite3    superlists    lists    manage.py

$ ls lists/tests.py
lists/tests.py

$ python manage.py test

Creating test database for alias 'default'...
...etc...

Kimitaka Nakazawa

unread,
Jun 9, 2014, 2:43:43 AM6/9/14
to django...@googlegroups.com
Hi Kelvin,

Thank you very much for your reply!
I tried your codes above but I still get the same error...

Kind regards,
Kim


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/46587e62-3642-4709-9ccf-ae91fec50d20%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kelvin Wong

unread,
Jun 9, 2014, 3:11:07 AM6/9/14
to django...@googlegroups.com
Find out which versions you are using

$ python --version
Python 2.7.6

$ django-admin.py version
1.4.10

Try importing the lists app from the shell

$ python manage.py shell

Python 2.7.6 (default, Jan 13 2014, 04:26:18) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import lists
>>> lists.__file__
'/users/you/superlists/lists/__init__.py'
>>> from lists.tests import *
>>>

If you see all that, run the lists app explicitly by label

$ python manage.py test lists

If that doesn't work, maybe there is something weird in your manage.py messing the paths. Paste it.

K

Kimitaka Nakazawa

unread,
Jun 9, 2014, 4:31:03 AM6/9/14
to django...@googlegroups.com, Kelvin Wong
Thank you very much for the reply!

Going through your suggestion below,
at ">>> from lists.tests import *” part, I got an error which is below. 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/kim/Desktop/Python/python3/superlists/lists/tests.py", line 3, in <module>
    from superlists.lists.views import home_page
ImportError: No module named lists.views

The code of the manage.py file is:
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "superlists.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Kind regards,
Kim

Kelvin Wong

unread,
Jun 9, 2014, 4:42:38 AM6/9/14
to django...@googlegroups.com
Open this file

/Users/kim/Desktop/Python/python3/superlists/lists/tests.py

On line 3 change

from superlists.lists.views import home_page

to

from lists.views import home_page

For the future, check your paths with the following

import sys; print(sys.path)

K

Kimitaka Nakazawa

unread,
Jun 10, 2014, 12:46:20 AM6/10/14
to django...@googlegroups.com
Thank you again for your advice!
It worked:) 

Kim


Reply all
Reply to author
Forward
0 new messages