Reputation:
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!
Upvotes: 2
Views: 1332
Reputation: 308899
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()
Upvotes: 1