131

I'm reading up that most people do from django.conf import settings but I don't undertstand the difference to simply doing import settings in a django project file. Can anyone explain the difference?

tzenderman
  • 2,523
  • 3
  • 21
  • 24

2 Answers2

152

import settings will import the first python module named settings.py found in sys.path. Usually (in default django setups) it allows access only to your site defined settings file, which overwrites django default settings (django.conf.global_settings).

So, if you try to access a valid django setting not specified in your settings file you will get an error.

django.conf.settings is not a file but an object (see source) making an abstraction of the concepts, default settings and your site-specific settings. Django also does other checks when you use from django.conf import settings.

You can also find it in the django docs.

starball
  • 20,030
  • 7
  • 43
  • 238
juliocesar
  • 5,706
  • 8
  • 44
  • 63
  • 7
    `import settings` will import the first python module named `settings.py` found in `sys.path`. This may not be the "site defined settings file", which is looked up in the environment variable "DJANGO_SETTINGS_MODULE" and can be just any python package or module. – bruno desthuilliers Nov 14 '13 at 12:02
  • @brunodesthuilliers thanks you. I update my answer taking aware of your correction. – juliocesar Nov 14 '13 at 12:16
  • 1
    Keeping `settings.py` next to `manage.py` (in the project root) is a default Django setup? O.o I believe the default one is to have it at `/settings.py`. At least that's where `django-admin startproject` puts it. – x-yuri Jan 07 '21 at 10:12
36

from django.conf import settings is better option.

I use different settings files for the same django project (one for "live", one for "dev"), the first one will select the one being executed.

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
mansuetus
  • 782
  • 5
  • 17