#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
)
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
- 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
You will get the error django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Since it's caused by Django, I think Django should be able to detect it and output a meaning error message
Thanks for this report, Can you provide a sample project? All my attempts raise an
ImportError
(even with circular imports inapps.py
)