Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

python - "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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...