Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Apps aren't loaded yet" and "django.core.exceptions.ImproperlyConfigured" in Django?

enter image description here

This is the directory structure of my Django project. When I am running python code of importing a model:from scraping.models import LinkVendorStandard from the file "framework_product_processing.py" it throws this exception:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

When I add this code: import django django.setup()

to initialize the django project settings, I get this exception: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I have the following 2 questions about this behavior:

  1. The file:"framework_product_process.py" in the django project structure is at the same level as "views.py" which can access model without having to setup the Django project.If this file is accessible from the same python path as that of view then why django.core.exceptions.ImproperlyConfigured?
  2. Even after adding import django;django.setup() code why I get django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.?

Can anyone please explain?

Update:enter image description here The file "framework_prodcut_processing.py" runs without any errors when I move it to a non_app python directory. non_app is not a Django app.

like image 610
javed Avatar asked Mar 28 '17 10:03

javed


1 Answers

django.core.exceptions.ImproperlyConfigured

When you run commands like python manage.py runserver, django automatically runs django.setup for you using DJANGO_SETTINGS_MODULE environment variable. So the code in views.py can access models, because django ensures that django.setup is called before views are imported. Since you are running your shell script as a simple python file, so you must manually call django.setup.

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet

This generally happens when your app gets imported before the complete settings files are imported (i.e. before the initialization of INSTALLED_APPS). So make sure you don't have any code in settings file, which imports code from some other apps.

Also ensure that you are not importing models or similar app code, in __init__.py files of your apps.

like image 90
hspandher Avatar answered Nov 02 '22 23:11

hspandher