41

I want to connect MySQL database to my django project, but it is throwing an error :

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

Trace:

 (myenv) LIBINGLADWINs-MacBook-Air:libinrenold$ django-admin dbshell
Traceback (most recent call last):
  File "/Users/libinrenold/Desktop/djangoworks/myenv/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/base.py", line 322, in execute
    saved_locale = translation.get_language()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 195, in get_language
    return _trans.get_language()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 59, in __getattr__
    if settings.USE_I18N:
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

settings.py.

  DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'user',
        'PASSWORD': 'root',
        'HOST':'',
        'PORT': '',
        }
     }
ppython
  • 485
  • 6
  • 19
reno_libiii
  • 525
  • 2
  • 5
  • 9

9 Answers9

42

You must define the relevant variable to show where your settings.py file lives:

export DJANGO_SETTINGS_MODULE=mysite.settings

This is the relevant docs entry:

When you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.

If you are using a virtualenv (which is the best practice), you can paste the relevant export command in the file <path-to-virtualenv>/bin/activate

raratiru
  • 8,748
  • 4
  • 73
  • 113
  • 2
    Hi thanks for your comment. I'm just a rookie, i always use virtualenv, now im getting the below error.. – reno_libiii Dec 08 '17 at 16:53
  • ModuleNotFoundError: No module named 'couzinz' -------------- but ive have added "import sys sys.path.append("/Users/../mysite")" – reno_libiii Dec 08 '17 at 16:57
  • @reno_libiii Please accept an answer since this question is answered and upvote the answers that gave you a good hint. Your comment introduces the need for another new question where you can paste the complete error message. – raratiru Dec 08 '17 at 23:18
  • What if you are just querying a database? I don't have a 'mysite' other then the REST I am querying – Brian Wiley May 17 '21 at 03:16
  • @BrianWiley "mysite" is actually the ``. The directory structure is `//settings.py`. This in python should be exported as: `project_name.settings` because `./manage.py` lies under the first ``. – raratiru May 19 '21 at 13:52
17

It happens to me too, I just now found unintentional.

I changed "configuration" in top of PyCharm IDE so pycharm confused when try to run django code. Exactly the problem popup when I try to run server with shortcut but when I used terminal to run server, I found django code not having any problem and pycharm can't found setting.

So please check "configuration" in top pycharm's ide maybe you do same mistake :)

I'm new in django so be calm if my answer was silly.

configuration picture

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
mehdi
  • 1,100
  • 1
  • 11
  • 27
16

I had this problem because I tried to import a django script after just typing

python

instead of

python manage.py shell

Duh!

Rick Graves
  • 517
  • 5
  • 11
  • 1
    Yep, my brain was off too and I was thinking it was something more complicated. Thanks for pointing this one out! – Daniel Aug 16 '21 at 15:06
  • Thanks, I spent too long racking my brain on this. I used `django-admin makemigrations users` instead of `python manage.py makemigrations users` – Gaberocksall Dec 04 '21 at 01:53
  • Thanks! Typing unconciously `python` by habit then it became a blind spot until someone points it out. – Katsum0to May 07 '22 at 05:48
6

Problem

I had the same error with Django==3.2 and djangorestframework==3.12.4 (2021/04) when I ran unittest. In my case, the python manage.py test works properly but I cannot directly run or debug the test module which was in a specific app, and I get this error:

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.

Solution

First I added the django project root to the sys.path. Then I added DJANGO_SETTINGS_MODULE to the environment variable to address the project setting root and called the Django setup function. This is my code:

from os import path, environ
from sys import path as sys_path
from django import setup

sys_path.append(<path to django setting.py>)    
environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')
setup()
r_agha_m
  • 310
  • 3
  • 6
4

put this in top of settings.py this will configure django for you

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", __file__)
import django
django.setup()
Naqib Hakimi
  • 874
  • 4
  • 15
4

I had the same error with Django 3.0.8 (july 2020) when trying to run the server, and in my case no need for the DJANGO_SETTINGS_MODULE environment variable. The solution is to use another form to run the server:

cd <directory containing manage.py>
python3 manage.py runserver

It works to run Django shell too :

python3 manage.py shell
Alain Cherpin
  • 305
  • 4
  • 5
3

Like raratiru answered, you need DJANGO_SETTINGS_MODULE environment variable defined with the relative pythonic path to your setting file.

OR use your django-admin command with the settings parameter:

django-admin --settings=mysite.settings dbshell
ppython
  • 485
  • 6
  • 19
  • now im getting a error as ModuleNotFoundError: No module named 'mysite' – reno_libiii Dec 08 '17 at 16:57
  • You'll need to replace `mysite` with the name of your project/folder where the settings file is in :) ! Maybe it's `djangoworks` looking from your post!? – ppython Dec 09 '17 at 02:48
  • 1
    the command should look like `django-admin dbshell --settings mysite.settings` to be accepted from the interpreter, see the link: [link](https://docs.djangoproject.com/en/1.11/ref/django-admin/#django-admin-and-manage-py) django-admin command and `$ django-admin help dbshell` – woodz Feb 20 '19 at 21:14
0

Below code in terminal to manually inform which settings I am using worked for me:

set DJANGO_SETTINGS_MODULE=mysite.settings
0

Dear all I faced the same problem and i scrapped the web to find a solution. None of the one above solved anything.

The issue in my case was related to a wrong configuration of pycharm. As for someone above the app didnt have any issue when launched from the command line/ shell.

The issue is here:

enter image description here

For some reason the env variable disappeared. i added back and everything started to work again without any issue.