Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured

Tags:

I'm trying to run a populate script which I put together from the tango_with_django tutorial (https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/populate_rango.py) however I'm getting the below traceback and it seems to be related to changes made in Django 1.7? I'd appreciate if someone could explain what I'm doing wrong here.

(test_env) C:\Users\WriteCode\test_env\epl>python populate_clubs.py Traceback (most recent call last):   File "populate_clubs.py", line 4, in <module>     django.setup()   File "c:\Python27\lib\site-packages\django\__init__.py", line 20, in setup     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)   File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __ge tattr__     self._setup(name)   File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _set up     % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, b ut settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.  (test_env) C:\Users\WriteCode\test_env\epl> 

My populate_clubs.py script

import os import sys import django django.setup()   def add_club(name, nickname, manager, established, stadium, active=True):     c = clubs.objects.get_or_create(name=name, nickname=nickname, manager=manager, established=established, stadium=stadium, active=active)     return c  def populate():     add_club(name = "Aston Villa",         nickname = "Villans",         manager = "Tim Sherwood",         established = "1897",         stadium = "Villa Park"         )      # Print out what was added     for c in clubs.objects.all():         print "The following has been added to clubs:" + c    # Start execution if __name__ == '__main__':     print "Starting club population script..."     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')     from teams.models import clubs     populate() 
like image 257
SkillSet12345 Avatar asked Feb 15 '15 12:02

SkillSet12345


2 Answers

You can insert os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") before the django.setup() line.

like image 154
bwangel Avatar answered Sep 19 '22 11:09

bwangel


Call to django.setup() should go after setting DJANGO_SETTINGS_MODULE environment variable. Just move it into your __main__ right after os.environ.setdefault().

like image 38
GwynBleidD Avatar answered Sep 18 '22 11:09

GwynBleidD