40

I've created a simple django 1.4 project and am trying to issue syncdb to create the (postgres) db schema. I'm getting this error :-

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured.Please supply the ENGINE value. Check settings documentation for more details.

My settings.py file looks like :-

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'test',                      # Or path to database file if using sqlite3.
        'USER': 'test',                      # Not used with sqlite3.
        'PASSWORD': 'test',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
     }
}

I can connect to the database using psql OK - any ideas ? Thanks in advance !

okm
  • 23,575
  • 5
  • 83
  • 90
bzo
  • 1,532
  • 7
  • 27
  • 40
  • 5
    What does the `DATABASES` line in the output of `./manage.py diffsettings` look like? – okm Apr 19 '12 at 13:37
  • 1
    DATABASES = {'default': {'ENGINE': 'django.db.backends.', 'NAME': '', 'HOST': '', 'USER': '', 'PASSWORD': '', 'PORT': ''}}. That looks incorrect right ? - how can that be fixed ? – bzo Apr 19 '12 at 15:13
  • if you are using some kind of virtual env, it could probably be that you forget to source your env... – Hanming Zeng Jul 25 '18 at 20:03

5 Answers5

29

It might be that django is not accessing the settings.py file you think it uses. Try explicitly point django to your settings file by using --settings

./manage.py --settings=nameofproject.settings runserver/syncdb

If this works then, you'll have to figure out why django is importing the wrong settings file.

Have you upgraded from 1.3 to 1.4 by accident ?

Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60
  • 2
    Thanks - all fixed. This was a 1.3 -> 1.4 upgrade issue. Cleaned out and reinstalled django and all was OK. – bzo Apr 20 '12 at 07:35
  • 1
    Another tip is to check your settings using: echo $DJANGO_SETTINGS_MODULE - if this is set to something unexpected it can cause this error. If so, you can test setting it just for this one command using: DJANGO_SETTINGS_MODULE=nameofproject.settings ./manage.py runserver – RichVel Mar 30 '13 at 12:44
  • @Jonas Please tell me the command for django 1.7 since syncdb doesn't work in it – Shravan Kumar Suthaar Feb 29 '16 at 18:16
  • 1
    I too found this doesn't work in newer versions of django, but perhaps the successor is `./manage.py diffsettings --settings=nameofproject.settings` @ShravanKumarSuthaar – rschwieb Apr 18 '16 at 17:49
  • Unknown command: '--settings=nameofproject.settings' – java-addict301 Jul 20 '22 at 19:15
17

For me, I had a similar problem with django 1.6 just now:

BACKGROUND

Django 1.6 heroku project using Heroku Postgresql database

I wanted to develop directly on the postgresql server (so don't copy the ".postgresql_psycopg2" bit if you're not also using postgresql)

  • No problem developing using the local psql db
  • Got that error when I uncommented the line to use heroku db

    DATABASES['default'] =  dj_database_url.config(default=os.getenv('DATABASE_URL'))  
    

Initial attempt was to add more details, such as another line,

DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'

This didn't work though, because then the error asked me for NAME, which it rejected.

SOLUTION

In the end, this solved it:

  1. I ran "heroku config" to see my details presented to me in the format:

    postgres://user:pass@localhost/dbname
    
  2. I updated the settings.py file to reflect those details:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', 
            'NAME': 'your_heroku_db_name',                     
            'USER': 'your_heroku_db_user_name',
            'PASSWORD': 'your_heroku_password',
            'HOST': 'ec2-23-21-133-106.compute-1.amazonaws.com', # Or something like this
            'PORT': '5432',                     
        }
    }
    

    That tip is from https://stackoverflow.com/a/19719966/870121

    Note: my next plan is to abstract these back into .env variables rather than leaving them visible in settings.py

  3. I then commented out the later line,

    # DATABASES['default'] =  dj_database_url.config(default=os.getenv('DATABASE_URL'))  
    

    so DATABASES was only specified once in the settings.py file

    That way the program read everything required to connect to the postgresql heroku db

    e.g., now python manage.py syncdb is working for me


If you want to try developing locally, comment out everything above and instead set your local postgresql server going and uncomment the local equivalent of the above:

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'cool01db',
         'USER': '', 
         'PASSWORD': '',
         'HOST': 'localhost', # '127.0.0.1' probably works also
         'PORT': '5432',
     }
 }

That's from https://stackoverflow.com/a/25962586/870121

Community
  • 1
  • 1
Mark
  • 1,285
  • 1
  • 19
  • 28
6

There are two 'settings.py' files in your project(if your OS is UNIX-like):

  • One is in the first dir
  • One is in the dir/dir

You need to write the ENGINE to the second (dir/dir/setting.py).

Good luck!

Tim Post
  • 33,371
  • 15
  • 110
  • 174
zwindy
  • 61
  • 1
  • 1
  • for PyCharm user, we might face this kind of problem when we move hierarchy of the whole project. No matter how many times we invalidate caches, the setting will stuck on the older path. For example, to run Django Test, we must include custom setting to the correct `settings.py`, that is inside the dir/dir as stated on this answer. Now it will work flawlessly. – Rahmat Nazali Salimi Jul 24 '20 at 04:40
3

Which settings.py file have you updated? I had this exact same problem at first too when I didn't read about Django's changes from 1.3 to 1.4 which causes double imports. Here is an excerpt from https://docs.djangoproject.com/en/1.4/releases/1.4/#updated-default-project-layout-and-manage-py. The latest version of Django (currently 1.4.2) ships with an updated default project layout and manage.py file for the startproject management command and the default project layout has changed.

To fix your error, the correct settings.py file you should use is NOT the one in the main project directory, it is the one inside the directory that is created inside the project directory (with the same name).

Desta Haileselassie Hagos
  • 23,140
  • 7
  • 48
  • 53
0

Re-installing Django did the trick for me(actually removing the egg file),as suggested in the following link. https://code.djangoproject.com/ticket/18058 It seems that upgrading from 1.3 to 1.4 seems to cause a lot of these problems.

Adel
  • 111
  • 8