Question

I don't know if this logic is correct, I'm trying to import a Django view in 2 different views. I have an import chain like this:

the

a.views import b.views
b.views import c.views
c.views import d.views

and

d.views import b.views

but when I reach the last step I get an ImportError.

If I put a comment in d.views avoiding the import of b.views, it works.

I'm new with Django, can somebody help me?

If I use in a.views and in d.views the syntax

from b.views import *

it works, but.. the code is not so readable.

If I use

from b.views import my_func

it doesn't work!

This is the error from django shell:

>>> import maps.views
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/save/sites/myblog/maps/views.py", line 19, in <module>
    from places.views import *
  File "/Users/save/sites/myblog/places/views.py", line 22, in <module>
    from posts.views import *
  File "/Users/save/sites/myblog/posts/views.py", line 31, in <module>
    from maps.views import render_map_geoloc
ImportError: cannot import name render_map_geoloc
Was it helpful?

Solution

Its because of cyclic dependency or circular reference.

b depends on c
c depends on d
d depends on b #which depends on c

Not sure for what purpose you are using. But you do explicit import to that function, and right above where its been used.

Looking at the error you are getting, it might be because of some dependency expected for d is coming from b so if you from b.views import *, it gets you that dependency. But if you import specific view (my_func), its missing that dependency.

Some more details you can find on SO answer thread - Django App Dependency Cycle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top