How to trace circular import error Django never solved!! Please sort this out.

369 views
Skip to first unread message

Sai

unread,
Jul 21, 2020, 8:08:57 PM7/21/20
to Django users
I am working Django rest framework api project, where I had multiple apps in the project. I got a circular import error when I am adding the app URLs to the main URLs. I Tried everything checked spellings, checked app structure but no use. Its been really frustrating with issue..please help to solve the problem. The app is working fine when I take off newly added line `path("covid/", include("Covid.urls")),`

I am using python3.6, django 3.0.4 and django-rest-framework 3.11.0

Here is project 
**urls.py** 

           
    from django.contrib import admin
    from django.conf.urls import url
    from django.urls import path, include
    from rest_framework_swagger.views import get_swagger_view
    
    schema_view = get_swagger_view(title='TmmrwInc API Documentation')
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        # path('', include('django.contrib.auth.urls')),
        # path('rest_auth/', include('rest_auth.urls')),
        path('api/password_reset/',
            include('django_rest_passwordreset.urls', namespace='password_reset')),
        # url(r'^invitations/', include('invitations.urls', namespace='invitations')),
        path('', include('rest_invitations.urls')),
        path("", include("UserAuth.urls")),
        path("doctors/", include("Administration.urls")),
        path("appointments/", include("Appointments.urls")),
        path("messaging/", include("messaging.urls")),
        path("meet/", include("meet.urls")),
        path("api_documentation/", schema_view),
        path("covid/", include("Covid.urls")),
    ]

This is app **urls.py** file

    from django.urls import path
    from . import views
    
    app_name = 'Covid'
    
    urlpatterns = [
        path('event/', views.EventBookingView.as_view()),
    ]

**settings.py** installed apps 

    ALLOWED_HOSTS = ['*']
    
    
    # Application definition
    
    INSTALLED_APPS = [
        "django.contrib.admin",
        "django.contrib.auth",
        "django.contrib.sites",
        "django.contrib.contenttypes",
        "django.contrib.sessions",
        "django.contrib.messages",
        "django.contrib.staticfiles",
        "UserAuth",
        "rest_framework",
        "corsheaders",
        'rest_framework.authtoken',
        'rest_auth',
        'Appointments',
        'messaging',
        'Administration',
        'channels',
        'invitations',
        'rest_invitations',
        'django_rest_passwordreset',
        'django_celery_beat',
        'meet',
        'rest_framework_swagger',
         'Covid',
    
    ]

This is a project structure

[Project Structure][1]


Mike Dewhirst

unread,
Jul 21, 2020, 8:29:25 PM7/21/20
to django...@googlegroups.com
On 22/07/2020 10:08 am, Sai wrote:
> I am working Django rest framework api project, where I had multiple
> apps in the project. I got a circular import error when I am adding
> the app URLs to the main URLs. I Tried everything checked spellings,
> checked app structure but no use. Its been really frustrating with
> issue..please help to solve the problem. The app is working fine when
> I take off newly added line `path("covid/", include("Covid.urls")),`

I can't help specifically but generally maybe. I have never been able to
trace circular imports. Instead I just remove them and drop them into
the methods which need them.

At first glance "from django.conf.urls import url" seems to be unused.
I'd try again without it or if I missed something you could add re_path
to the django.urls imports.

Hope I haven't steered you down a blind alley.

M
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/90296ccf-c893-4cf1-b57a-2da47396a2bbo%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/90296ccf-c893-4cf1-b57a-2da47396a2bbo%40googlegroups.com?utm_medium=email&utm_source=footer>.

signature.asc

Chaitanya Sai

unread,
Jul 21, 2020, 9:31:28 PM7/21/20
to django...@googlegroups.com
Can any one Look into this thing ??????

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/90296ccf-c893-4cf1-b57a-2da47396a2bbo%40googlegroups.com.

Chaitanya Sai

unread,
Jul 22, 2020, 10:13:37 AM7/22/20
to django...@googlegroups.com
Hello Django users, 
Is there not even single person who can solve this issue??????????????


On Tue, Jul 21, 2020 at 8:09 PM Sai <chaitu....@gmail.com> wrote:
--

sandeep kumar

unread,
Jul 22, 2020, 10:22:44 AM7/22/20
to django...@googlegroups.com
I have faced this issue atleast 100 times..:)

Her are the steps (sorry, I could not find a better way):

1. comment urls in urls.py (inside individual app)
2. check if you still have issue
2a. if you do not have issue, then you probably have not defined views for some of the urls
2b. if you still see the error, comment 'impot views' statement.
3a. if the issue is gone after commenting 'import views' then check views.py file, it must have some typos (e.g., misalignment, accidental copying of some special character etc.)
3b. if the issue is not gone even after commenting 'import views', let me know. I am not sure I have reached that stage ever..:)

Hope this helps.

Thanks,
Sandeep

--
Dr. Sandeep Kumar,
Postdoctoral Researcher,
Lunenfeld Tanenbaum Research Institute,
Mount Sinai Hospital,
600 University Ave,
Toronto, Ontario
Canada


Liu Zheng

unread,
Jul 22, 2020, 11:22:48 AM7/22/20
to django...@googlegroups.com
Hi,

Probably not enough to look at urls.py files only. My guess: Do you use ‘reverse’ function anywhere in covid app? If so, here’s a circular import:

urls.py imports covid urls -> covid urls import covid views -> one covid view uses reverse -> reverse import urls.py of the project (to look up the url with the end point name).

A quick fix will be using lazy_reverse instead of reverse.

Chaitanya Sai

unread,
Jul 22, 2020, 11:29:31 AM7/22/20
to django...@googlegroups.com
Thanks for your reply guys. I figured it out. I have typo of serializer name in serilizers.py which is causing the error.
Damn, I spend 4 hours into this. 

Reply all
Reply to author
Forward
0 new messages