trex trex - 1 year ago 208
Python Question

Writing and running tests. ImportError: No module named generic

I'm new to tests in Django. And I need to write a couple.

Django version 1.9.7.

Linux version 4.2.0-42-generic (buildd@lgw01-54) (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) ) #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016

My simple test code is:

cat animal/

from django.test import TestCase
from animal.models import Animal

class AnimalTestCase(TestCase):
def say_hello(self):
print('Hello, World!')

I execute it in this way
./ test animal

And the following error arise:

Traceback (most recent call last):
File "./", line 13, in <module>
File "/path-to-venv/local/lib/python2.7/site-packages/django/core/management/", line 353, in execute_from_command_line
File "/path-to-venv/local/lib/python2.7/site-packages/django/core/management/", line 327, in execute
File "/path-to-venv/local/lib/python2.7/site-packages/django/", line 18, in setup
File "/path-to-venv/local/lib/python2.7/site-packages/django/apps/", line 85, in populate
app_config = AppConfig.create(entry)
File "/path-to-venv/local/lib/python2.7/site-packages/django/apps/", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/", line 37, in import_module
File "/path-to-venv/local/lib/python2.7/site-packages/autofixture/", line 5, in <module>
from autofixture.base import AutoFixture
File "/path-to-venv/local/lib/python2.7/site-packages/autofixture/", line 7, in <module>
from django.contrib.contenttypes.generic import GenericRelation
ImportError: No module named generic

What am I doing wrong?

Answer Source

Your installed version of django-autofixture does not support Django 1.9, because it has out of date imports for GenericRelation.

Try upgrading to the latest version. The project's changelist says that Django 1.9 support was added in version 0.11.0.

In order for Django to run your method in your AnimalTestCase, you need to rename it so that it begins with test_:

class AnimalTestCase(TestCase):
    def test_say_hello(self):
        print('Hello, World!')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download