10

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.

Javed
  • 5,904
  • 4
  • 46
  • 71

1 Answers1

15

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.

hspandher
  • 15,934
  • 2
  • 32
  • 45
  • 1
    your answer helped me to understand first question, thanks for that. But about the second question I think importing app before importing settings files does not apply here because in the file "framework_product_process.py" begins with `import django;django.setup()`. I made it work by moving out the file in question to a python directory - please see the update in question. – Javed Mar 28 '17 at 11:48
  • 1
    When you run `django.setup`, settings files are imported, which probably lead to the case I mentioned in my answer – hspandher Mar 28 '17 at 11:49
  • 1
    That last sentence/paragraph is confusing to me... isn't the entire purpose of an `__init__.py` file to have import statements in it? What are you suggesting we have in that file instead, if we're not going to import stuff in it? – ArtOfWarfare May 30 '21 at 17:36
  • 1
    @ArtOfWarfare Well, ideally that's what "__init___" is for to add public imports of the module. However, in case of Django apps, it also has tendency to cause circular imports and premature model imports because init file is parsed when an app is imported – hspandher Jun 05 '21 at 01:26
  • @hspandher Do you know how it can be done? [Hiding exact location of classes and functions in a Django app (similar to how __init__.py works)](https://stackoverflow.com/questions/71657149/hiding-exact-location-of-classes-and-functions-in-a-django-app-similar-to-how) – Amir Shabani Mar 29 '22 at 06:02