[Django]-Django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. (django 2.0.1)(Python 3.6)

57๐Ÿ‘

Overcame similar situation just now.

All you really need is this:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")

And then these lines:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

After that you can easily import models without AppRegistryNotReady: Apps aren't loaded yet.

UPDATE: This is really exactly the 4 code lines from wsgi.py file in your projectโ€™s folder.

FOR DJANGO 3.0
In Django 3+ an extra variable is needed to resolve sync/async confusing:

os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
๐Ÿ‘คAlexey Trofimov

34๐Ÿ‘

Please run check django-admin command to see if it have detected any errors.

python manage.py check

and/or

django-admin check

8๐Ÿ‘

My problem was that I was trying to import before the setup was ran. Hereโ€™s my solution: make the import after the setup:

import django

# some variable declarations    
world_mapping = {
    'osm_id': 'osm_id',
}    

if __name__ == '__main__':
    django.setup()
    # import AFTER setup
    from app.models import WorldBorder
    # from now I can access WorldBorder!!
๐Ÿ‘คOlivier Pons

7๐Ÿ‘

The Django docs say that django.setup loads the settings from settings.py as its first act, so it seems like a bad idea to run that in settings.py.

Try commenting out apps in INSTALLED_APPS one at a time โ€“ youโ€™ll probably find that one of them is not loading for some reason. Once you know which one it is, you can work out what is wrong with it.

๐Ÿ‘คGlenn

3๐Ÿ‘

Just import inside ready() method:

def ready(self):
    print('Sent From ready')
    from django.db.models.signals import post_save
    from yourapp.api.signals import post_save_user_receiver
    post_save.connect(post_save_user_receiver, sender=settings.AUTH_USER_MODEL)
๐Ÿ‘คMahmoud Masri

3๐Ÿ‘

In your wsgi.py or asgi.py as the case may be

have these lines before anything else

import os 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

import django
django.setup()
๐Ÿ‘คMcConnell

2๐Ÿ‘

Just in case it helps someone: my issue was that I was importing some classes, functions and variables into the __init__ file of the appโ€™s folder.

It work as expected after I empty the __init__ file.

2๐Ÿ‘

I had similar problem with Apps aren't loaded yet. in Django 2.0

Problem was started here with importing modules outside def ready()

from django.apps import AppConfig

from django.contrib.auth.models import User
from django.db.models.signals import post_delete
from .signals import post_delete_msg

class ProductionAppConfig(AppConfig):
    name = 'production_app'

    def ready(self):
        post_delete.connect(post_delete_msg, sender=User)

solved by moving imports to def ready()

from django.apps import AppConfig
    
class ProductionAppConfig(AppConfig):
    name = 'production_app'

    def ready(self):
        from django.contrib.auth.models import User
        from django.db.models.signals import post_delete
        from .signals import post_delete_msg
        post_delete.connect(post_delete_msg, sender=User)
        
๐Ÿ‘คRobert

2๐Ÿ‘

import django
django.setup()

This worked for meโ€ฆI tried everything and at last moment, this two lines solved my issue.

๐Ÿ‘คKiran

1๐Ÿ‘

There could be several reasons for this error but all of them are related to project/settings.py file.

  1. Check if you have initialised SECRET_KEY in it.
  2. Check if you have application in INSTALLED_APPS and is not installed.
๐Ÿ‘คRohit Sehgal

1๐Ÿ‘

First at all: check if you have the same code like below in yourproject.wsgi.py

"""
WSGI config for store project.

It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings")

application = get_wsgi_application()

Like mentioned this config is for django2.0, seek to have
the rigth code for your version.

THEN
Type this below code in your ~/.basrc or ~/.zshrc for zsh, anyway type this code in your rigth shell file.

export SECRET_KEY="type_a_long_random_char_printable_here"
#like this: export SECRET_KEY="hjfhskjh(@/;,?jhod=sjhGJKghgjGHJh#=}"

happened me for django deployment on heroku , after over checked, checked again your SECRET_KEY=โ€remove here all char like those: $\` โ€œ and you havโ€™nt specified any directory that doesโ€™nt exist.

๐Ÿ‘คyattara

0๐Ÿ‘

It took me a while to understand that every time you run manage.py somecommand, you need to provide the same settings / environment variables that you need when you run ./manage.py runserver.

For example I load SECRET_KEY in from an environment variable in a file called .env. So I need to do this in order to make and run migrations:

. .env
./manage.py makemigrations --settings=djangoproject.settings.development
./manage.py migrate --settings=djangoproject.settings.development
๐Ÿ‘คLittle Brain

0๐Ÿ‘

In my case it was a missing python package which was in use in the application that caused the issue.

So check if all applications / packages in use are effectively installed in your python (virtual) environment.

The applications you can find in the INSTALLED_APPS (see your settings.py file).

Python packages can be used anywhere in your code so can be more difficult to trace down. But usually the error message will hint at the missing package as well.

๐Ÿ‘คDavy

0๐Ÿ‘

I uninstalled and reinstalled Django to fix this issue.
pip uninstall django

Then pip install django.

Note: install the same django version as before.

๐Ÿ‘คImportError

0๐Ÿ‘

make sure everything in your app runs after apps.py. check to not importing any module in apps.py except AppConfig (or any django bult in module)

๐Ÿ‘คHamed Damirchi

0๐Ÿ‘

Try to change DEBUG settings to False

DEBUG = False

This solved my error

๐Ÿ‘คDigamber Jha

0๐Ÿ‘

Happened to me when I did

from unittest import TestCase, mock

instead of

from unittest import mock

from django.test import TestCase

in my test file

๐Ÿ‘คWojtek

0๐Ÿ‘

place this on the top of the "asgi.py" file (there are conflicts between jwt auth, because it is accessing the user model before get_asgi_application function):

django_asgi_app = get_asgi_application()
๐Ÿ‘คdar rut

0๐Ÿ‘

Make sure there are no broken imports at the beginning of your settings.py file.
Hope this answer saves someoneโ€™s day

๐Ÿ‘คjuvet manga

0๐Ÿ‘

In my case,This error message was caused because of an unused import in the core/settings.py file.

NOTE: Delete unused imports in you project main settings file.

๐Ÿ‘คRafaell444

Leave a comment