Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#31560 closed Bug (needsinfo)

Circular imports raise AppRegistryNotReady.

Reported by: ExTexan Owned by: nobody
Component: Core (Other) Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I just spent an inordinate amount of time debugging an "Apps aren't loaded yet" issue. Googling wasn't much help as I found posts offering "possible causes" all over the spectrum. Finally, after repeated searches with different keywords, I just happened to hit on the right solution.

It turned out to be caused by circular imports. I can't even imagine how circular imports would cause the "Apps aren't loaded" exception, but needless to say, that error message doesn't describe the actual situation.

If possible, could circular imports be detected and an appropriate response given?

Change History (3)

comment:1 by Mariusz Felisiak, 5 years ago

Component: Error reportingCore (Other)
Owner: set to nobody
Resolution: needsinfo
Status: newclosed
Summary: Detect circular imports and give appropriate messageCircular imports raise AppRegistryNotReady.

Thanks for this report, Can you provide a sample project? All my attempts raise an ImportError (even with circular imports in apps.py)

comment:2 by ExTexan, 5 years ago

In one of my cases, it was a model imported in a signals.py and the signals.py imported by the same model it imported. Then anything that tried to import the model would encounter the circular imports. As a quick test, I just went back to the signals.py file and put the model import back in. I got ImportError, as you did. There were a slew of changes I made when I fixed the "Apps aren't loaded yet" issue, so now I can't be sure what actually caused it in the first place.

I'll need to revert that entire commit and take it one step at a time to see if I can zero in on the cause. I'll see if I can make time to do this, but workload is heavy atm, so I can't really say when I'll get to it.

in reply to:  1 comment:3 by HMaker, 4 years ago

Replying to felixxm:

Thanks for this report, Can you provide a sample project? All my attempts raise an ImportError (even with circular imports in apps.py)

I had this problem and managed to debug it right now. It will happen when Django's app registry imports the app's module added in INSTALLED_APPS and that module imports, directly or indirectly, a model of the app being loaded . Reading the docs https://docs.djangoproject.com/en/3.0/ref/applications/ there are no notes warning about it.

Steps to reproduce

  1. Create a django project and one app
  2. Create a Model in app/models.py
  3. In app/__init__.py import the created model
  4. Add the app's dotted path to INSTALLED_APPS
  5. Try to run with ./manage.py runserver

You will get the error django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. . At a model's import time, the model metaclass will check whether its app is ready, but it is not, so the AppRegistryNotReady will be raised since the registry is being populated.

Since it's caused by Django, I think Django should be able to detect it and output a meaning error message

Version 1, edited 4 years ago by HMaker (previous) (next) (diff)
Note: See TracTickets for help on using tickets.
Back to Top