#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)
follow-up: 3 comment:1 by , 5 years ago
Component: | Error reporting → Core (Other) |
---|---|
Owner: | set to |
Resolution: | → needsinfo |
Status: | new → closed |
Summary: | Detect circular imports and give appropriate message → Circular imports raise AppRegistryNotReady. |
comment:2 by , 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.
comment:3 by , 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 inapps.py
)
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.
Steps to reproduce
- Create a django project and one app
- Create a Model in app/models.py
- In
app/__init__.py
import the created model - Add the app's dotted path to INSTALLED_APPS
- Try to run with
./manage.py runserver
Thanks for this report, Can you provide a sample project? All my attempts raise an
ImportError
(even with circular imports inapps.py
)