Opened 10 years ago

Closed 10 years ago

#7387 closed (fixed)

Recursive imports in django.contrib.contenttypes.generic

Reported by: oliver@… Owned by: nobody
Component: Contrib apps Version: master
Severity: Keywords: import importerror recursive contenttypes
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I'm trying to import django.contrib.contenttypes.generic in a script of mine, which causes a recursive import in Django:-

>>> from django.contrib.contenttypes import generic
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.5/site-packages/django/contrib/contenttypes/", line 8, in <module>
    from django.db.models import signals
  File "/Library/Python/2.5/site-packages/django/db/models/", line 6, in <module>
    from django.db.models.query import Q
  File "/Library/Python/2.5/site-packages/django/db/models/", line 7, in <module>
    from django.db.models import signals, sql
  File "/Library/Python/2.5/site-packages/django/db/models/sql/", line 2, in <module>
    from subqueries import *
  File "/Library/Python/2.5/site-packages/django/db/models/sql/", line 5, in <module>
    from django.contrib.contenttypes import generic
ImportError: cannot import name generic

However, if I try this in Django's shell (as in through shell this seems to work fine for some reason. However, in a standard Python shell when I've run setup_environ it doesn't, and I see this error. It's important for me to be able to use it in the standard Python shell as this was highlighted to me by a script I'm trying to write).

Change History (4)

comment:1 Changed 10 years ago by oliver@…

Summary: Recursive imports in django.contib.contenttypes.genericRecursive imports in django.contrib.contenttypes.generic

comment:2 Changed 10 years ago by George Vilches

By coincidence I had the exact same error occur today. For a short term fix, do the imports in this order:

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

I'm not saying you should have to, just that it seems to work for setup_environ() for me.

comment:3 Changed 10 years ago by Jeff Anderson

Triage Stage: UnreviewedAccepted

comment:4 Changed 10 years ago by Gary Wilson

Resolution: fixed
Status: newclosed

(In [7635]) Fixed #7387 - Fixed circular import problem when importing contrib.contenttypes.generic module.

Note: See TracTickets for help on using tickets.
Back to Top