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

时间:2018-03-08 16:04:12

标签: python django python-3.x docker

我正在尝试运行一个容器内的python文件但是我得到以下异常:

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.

代码在docker中运行:

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

我的代码:

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

我是django和docker的新手,我可以告诉我需要设置环境然而我不知道如何通过manage.py为你做这件事,所以如果我要运行一个通过解释器的python文件,我必须手动完成,但我不知道如何。

我还读到我需要以某种方式管理码头工具环境,但我不确定这意味着什么,感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

由于错误建议您需要设置DJANGO_SETTINGS_MODULE环境变量。它的值应该类似于mysite.settings(检查manage.py中的内容。)

您可以在Dockerfile

中进行设置
ENV DJANGO_SETTINGS_MODULE mysite.settings

或者在您

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

或者在剧本中。

import os

import django

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

django.setup()