django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured
Question:
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:UsersWriteCodetest_envepl>python populate_clubs.py
Traceback (most recent call last):
File "populate_clubs.py", line 4, in <module>
django.setup()
File "c:Python27libsite-packagesdjango__init__.py", line 20, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "c:Python27libsite-packagesdjangoconf__init__.py", line 46, in __ge
tattr__
self._setup(name)
File "c:Python27libsite-packagesdjangoconf__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:UsersWriteCodetest_envepl>
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()
Answers:
Call to django.setup()
should go after setting DJANGO_SETTINGS_MODULE
environment variable. Just move it into your __main__
right after os.environ.setdefault()
.
You can insert os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
before the django.setup()
line.
If you are getting a similar error after initiating your interaction with Django by running python
in a terminal, running python manage.py shell
in the appropriate directory may solve your issue.
For development and debugging, you may use the standalone python package.
Install with pip
pip install standalone
Use standalone to use Django from the module.
# add the following to the top of the module.
import standalone
standalone.run('mysite.settings') # replace with your settings module.
# your code goes below the configuration
import os
import sys
# ... .. . whole module
Now you may run the module as a Python Django script.
It happened to me when I used django related import statement in a non-django module.
i.e from index.views import view
that did raise an error for me.
I met the same problem when setting the environment.
(I tried to add the:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
into the “Starting script window”,which is in the Django Console or rewrite the .wsgi file, But all of them were failed)
My final solution: (open the terminal and set the environment manually)
Here are the steps:(for mac)
-
open the terminal and type vim ~/.bash_profile
-
add below lines:
PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin
PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name]
DJANGO_SETTINGS_MODULE=[project_name].settings
-
type :wq to save and exit terminal
-
type source ~/.bash_profile to reload it
it will work when you run python manage.py
shell this time.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
should be above
import django
which is you should call django_settings_module before importing and calling django setup.
Hope this is helpful.
If you’re running PyCharm, what worked for me was to invalidate the cache and restart the app.
File => Invalidate Caches / Restart ...
My virtualenv had been recently updated from Python 3.6 or 3.7 to 3.8.
Make sure to activate your venv first by scripts/activate venv then in your populate_user.py right after import os run os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘epl.settings’) this should work.
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:UsersWriteCodetest_envepl>python populate_clubs.py
Traceback (most recent call last):
File "populate_clubs.py", line 4, in <module>
django.setup()
File "c:Python27libsite-packagesdjango__init__.py", line 20, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "c:Python27libsite-packagesdjangoconf__init__.py", line 46, in __ge
tattr__
self._setup(name)
File "c:Python27libsite-packagesdjangoconf__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:UsersWriteCodetest_envepl>
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()
Call to django.setup()
should go after setting DJANGO_SETTINGS_MODULE
environment variable. Just move it into your __main__
right after os.environ.setdefault()
.
You can insert os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
before the django.setup()
line.
If you are getting a similar error after initiating your interaction with Django by running python
in a terminal, running python manage.py shell
in the appropriate directory may solve your issue.
For development and debugging, you may use the standalone python package.
Install with pip
pip install standalone
Use standalone to use Django from the module.
# add the following to the top of the module.
import standalone
standalone.run('mysite.settings') # replace with your settings module.
# your code goes below the configuration
import os
import sys
# ... .. . whole module
Now you may run the module as a Python Django script.
It happened to me when I used django related import statement in a non-django module.
i.e from index.views import view
that did raise an error for me.
I met the same problem when setting the environment.
(I tried to add the:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
into the “Starting script window”,which is in the Django Console or rewrite the .wsgi file, But all of them were failed)
My final solution: (open the terminal and set the environment manually)
Here are the steps:(for mac)
-
open the terminal and type vim ~/.bash_profile
-
add below lines:
PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name] DJANGO_SETTINGS_MODULE=[project_name].settings
-
type :wq to save and exit terminal
-
type source ~/.bash_profile to reload it
it will work when you run python manage.py
shell this time.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
should be above
import django
which is you should call django_settings_module before importing and calling django setup.
Hope this is helpful.
If you’re running PyCharm, what worked for me was to invalidate the cache and restart the app.
File => Invalidate Caches / Restart ...
My virtualenv had been recently updated from Python 3.6 or 3.7 to 3.8.
Make sure to activate your venv first by scripts/activate venv then in your populate_user.py right after import os run os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘epl.settings’) this should work.