django os.environ 설정 문제

django os.environ 설정 문제

python에서 특정 module을 import 해서

사용하고자 할 때, 다음과 같은 에러가 나는

경우가 있다.

Error 1

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.ent

Issue

원인은 Django에 기본적인 module setting이 안되어

있기 때문이다.

solution

먼저 os module을 import 해주고,

기본 셋팅을 default로 설정해준다.

django 모듈을 import 해주고,

설정된 default값을 반영해준다.

import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ProjName.settings")

import django django.setup()

모듈 사용이 정상적으로 되는 것을 볼 수 있다.

from http://incomeplus.tistory.com/164 by ccl(A) rewrite - 2021-10-02 19:01:14