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.`

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.

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.