Running django tutorial tests fail - No module named polls.tests
For anyone else having the same problem, another reason for this to happen is if you have the same name for the root folder and the project folder.
For example:
mydjango
├── __init__.py
├── manage.py
├── mydjango
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
├── polls
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
| ├── tests.py
│ ├── templates
running
./manage.py test
throws errors No module named polls.tests
to fix it simply rename the root folder to something else like :
mydjango_project
├── __init__.py
├── manage.py
├── mydjango
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
├── polls
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
| ├── tests.py
│ ├── templates
I had exactly the same issue with my Django project:
$ python manage test polls.tests
worked fine whereas the following failed with an import error:
$ python manage test polls
$ python manage test
(...)
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
(...)
ImportError: No module named polls.tests
Check carefully the error message: Django's test runner tries to import the tests from mydjango.polls.tests where mydjango is the name of the root directory (the container for your project).
I fixed this issue by deleting the __init__.py
file in mydjango directory (at the same level than manage.py file). This directory is not supposed to be a python module and it seems to mess up with Django's test runner if it is the case.
So just deleting the _init_.py file should fix our problem:
$ rm mydjango/__init__.py