Opened 4 years ago

Closed 4 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, 4 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, 4 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

Edit: Django docs https://docs.djangoproject.com/en/3.0/ref/applications/#initialization-process states that models must not be imported during apps population time.
So the OP should check whether models are being imported during that process.

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
Last edited 4 years ago by HMaker (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top