Python Shell Apps aren't loaded yet

I am having the following issue when i try to write queries using python shell

from feed.models import video
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/app/feed/models.py", line 1, in <module>
    from users.models import NewUser, category
  File "/app/users/models.py", line 1, in <module>
    from django.contrib.auth.models import AbstractUser
  File "/usr/local/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 72, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

then i solved this issue by writing this command in the terminal

export DJANGO_SETTINGS_MODULE=project.settings

then i got the following error

`from users.models import NewUser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/app/users/models.py", line 1, in <module>
    from django.contrib.auth.models import AbstractUser
  File "/usr/local/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.`
1 Like

This could be an issue with your Django settings, for example an app in INSTALLED_APPS that didn’t exist. Once it was removed, it resolved the exception. Apps that can’t be imported for any reason will also raise an AppRegistryNotReady exception.

1 Like

All apps in INSTALLED_APPS are working

Three things to check:

  1. Your current directory in the shell is the base directory of your project.

  2. Your virtual environment for your project is active.

  3. Are you trying to perform any ORM or database operations at the module level in your models.py file?

Also, please post your INSTALLED_APPS setting.

1 Like

Hi,
I got the same issue, there are my INSTALLED_APP and my models.py :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "app.apps.AppConfig",
    'corsheaders',
    'django.core'
]
from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    email = models.CharField(max_length=255, unique=True)
    password = models.CharField(max_length=255)
    bio = models.TextField(blank=True)
    date_of_birth = models.DateField(null=True, blank=True)
    profile_picture = models.ImageField(upload_to='profile_pics/', null=True, blank=True)

    first_name = None
    last_name = None
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []

  • django.core should not be an entry in INSTALLED_APPS.

  • What version of Python are you using? What version of Django?

  • What are the contents of your app.apps.AppConfig file?

  • Please post the complete traceback from the error you are receiving.

1 Like

Here is my complete traceback from the error I recieved :

Traceback (most recent call last):
  File "/home/yan/Documents/projet-py/applitest/manage.py", line 22, in <module>
    main()
  File "/home/yan/Documents/projet-py/applitest/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/yan/.local/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/yan/.local/lib/python3.10/site-packages/django/core/management/__init__.py", line 382, in execute
    settings.INSTALLED_APPS
  File "/home/yan/.local/lib/python3.10/site-packages/django/conf/__init__.py", line 89, in __getattr__
    self._setup(name)
  File "/home/yan/.local/lib/python3.10/site-packages/django/conf/__init__.py", line 76, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/yan/.local/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/yan/Documents/projet-py/applitest/applitest/settings.py", line 18, in <module>
    import app.models
  File "/home/yan/Documents/projet-py/applitest/app/models.py", line 4, in <module>
    from django.contrib.auth.models import AbstractUser
  File "/home/yan/.local/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/yan/.local/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 58, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/yan/.local/lib/python3.10/site-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/yan/.local/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/yan/.local/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I’m using the 3.10.6 version of Python and 5.0 of Django
My app.apps.AppConfig file content is :

class AppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'app'

Thank you for your response

Please show your settings.py file. (Feel free to obfuscate or remove sensitive values like secret keys or database passwords.)

1 Like

Here is my settings.py file :

"""
Django settings for applitest project.

Generated by 'django-admin startproject' using Django 5.0.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path

import django.core.exceptions
from django import core

import app.models
import applitest

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-^1((po#y3ent(6^rn4*3qrwdi@^ab^8+-u&q93c-6v#5^4tu73'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "app.apps.AppConfig",
    'corsheaders',
    'django.core'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'applitest.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'applitest.wsgi.application'

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '******',
        'USER': '*****',
        'PASSWORD': '*****',
        'HOST': '******',
        'PORT': '******',
    }
}

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'app.User'

(Happy new Year)

This is the problem. You cannot import any models into the settings file. (This also means that you cannot use any models in your settings, but I don’t see any instances of that here.)

2 Likes

Thank you very much for your help !

hi.
please i’m getting a similar error while running my code.

C:\Users\ftefo\PycharmProjects\pythonProject1.venv\Scripts\python.exe C:\Users\ftefo\PycharmProjects\pythonProject1\myproject\myapp\models.py

Traceback (most recent call last):
File “C:\Users\ftefo\PycharmProjects\pythonProject1\myproject\myapp\models.py”, line 11, in
class MotorData(models.Model):
File “C:\Users\ftefo\PycharmProjects\pythonProject1.venv\lib\site-packages\django\db\models\base.py”, line 129, in new
app_config = apps.get_containing_app_config(module)
File “C:\Users\ftefo\PycharmProjects\pythonProject1.venv\lib\site-packages\django\apps\registry.py”, line 260, in get_containing_app_config
self.check_apps_ready()
File “C:\Users\ftefo\PycharmProjects\pythonProject1.venv\lib\site-packages\django\apps\registry.py”, line 138, in check_apps_ready
raise AppRegistryNotReady(“Apps aren’t loaded yet.”)
django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.

Process finished with exit code 1

Side note: When posting code, templates, or error messages here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Please see the post above at Python Shell Apps aren't loaded yet - #4 by KenWhitesell

ok thanks. actually new here and also new with django.

this is my INSTALLED_APPS:

 INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.core',
    'myapp.apps.MyappConfig',
]

Welcome!

That addresses one of the questions, what about the other two?

Also, why do you have django.core in your INSTALLED_APPS? It’s not an app.

You mention that you’re new to Django - have you worked your way through either the Official Django Tutorial or the Django Girls Tutorial? They’re the best places to get started.

I saw the django.core in the previous persons intalled_apps, so I added it hoping it could help.
here is the current directory of my shell, and it seems to me that my virtual environment is activated :
(.venv) PS C:\Users\ftefo\PycharmProjects\pythonProject1\myproject>

i have worked through the the Official Django Tutorial and some youtube tutorials also but i couldn’t fne any solution for the current error

How are you running your project?

i’m running my project using the run button in pycharm
hope this answers the question

What command is it issuing?