Python | Docker | Django | django.core.exceptions.ImproperlyConfigured Error

JEff

I'm trying to run a python file that is inside a container however I'm getting the following exception:

Traceback (most recent call last):
  File "/tmp/e2esetup.py", line 2, in <module>
    django.setup()
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Code being run in docker:

docker exec -it $DOCKER_ID python /tmp/e2esetup.py

My code:

import django
django.setup()
from django.conf import settings
from django.contrib.auth.models import User
from apps.account.models import Profile
from apps.organisation.models import Organisation
.... 
#rest is fine for sure

I'm new to django and docker, from what I can tell I need to set the environment however I don't know how, running through manage.py does this for you or something, so if I were to run a python file through the interpreter, I have to do this manually, however I dont know how.

I also read that I need to pipe the docker environment somehow, but I'm not sure what that means either, any help is appreciated!

Alasdair

As the error suggests you need to set the DJANGO_SETTINGS_MODULE environment variable. It's value should be something like mysite.settings (check what it is in manage.py).

You can set it in the Dockerfile

ENV DJANGO_SETTINGS_MODULE mysite.settings

Or in the environment when you

docker exec -it -e "DJANGO_SETTINGS_MODULE=mysite.settings" $DOCKER_ID python /tmp/e2esetup.py

Or in the script itself.

import os

import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 

django.setup()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django Installed app and ImproperlyConfigured Error: django.core.exceptions.ImproperlyConfigured

django.core.exceptions.ImproperlyConfigured

Django.core.exceptions.ImproperlyConfigured:

How to fix the error for django 'django.core.exceptions.ImproperlyConfigured' with urls?

Issue in django.core.exceptions.ImproperlyConfigured

Django Error: django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module

Django Celery scheduled task django.core.exceptions.ImproperlyConfigured

django.core.exceptions.ImproperlyConfigured: Django 3.0.1 is not supported

Django channels pytest testing. django.core.exceptions.ImproperlyConfigured. .

Django.core.exceptions.ImproperlyConfigured: Error running functional_tests.py

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Error message: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty

django.core.exceptions.ImproperlyConfigured: WSGI application 'DjBlog.wsgi.application' could not be loaded; Error importing module

django.core.exceptions.ImproperlyConfigured: Field name `id` is not valid for model

django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG

django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library

"django.core.exceptions.ImproperlyConfigured:" while doing migrations to posgreSQL

Error in deploying Django app on Google cloud using appengine "django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable"

django.core.exceptions.ImproperlyConfigured: WSGI application '{project_name}.wsgi.application' could not be loaded; Error importing module

django.core.exceptions.ImproperlyConfigured: uses parameter name 'order.id' which isn't a valid Python identifier

How to resolve "django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: foo" in Django 1.7?

django.core.exceptions.ImproperlyConfigured: Enable 'django.contrib.auth.context_processors.auth'

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

Can't run django-admin startproject mysite (ubuntu, django.core.exceptions.ImproperlyConfigured)

django.core.exceptions.ImproperlyConfigured: Cannot import ASGI_APPLICATION module 'routing'

DRF throw django.core.exceptions.ImproperlyConfigured when override get_queryset

django.core.exceptions.ImproperlyConfigured: Cannot import 'category'. Check that 'api.category.apps.CategoryConfig.name' is correct