103

What is the basic difference between the following import statements in a Django app?

import settings

and

from django.conf import settings
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • 1
    +1 because it is a very interesting questions for who is new to Django development. –  Jan 08 '12 at 20:28

1 Answers1

138
import settings

Will import settings(.py) module of your Django project (if you are writing this code from the "root" package of your application, of course)

from django.conf import settings

Will import settings object from django.conf package (Django's provided files). This is important, because

[..] note that your code should not import from either global_settings or your own settings file. django.conf.settings abstracts the concepts of default settings and site-specific settings; it presents a single interface. It also decouples the code that uses settings from the location of your settings.

UPDATE: if you want to define some own settings, see this part of the documentation

  • 4
    FYI, global_settings is another module inside django.conf package. As the documentation says, do not use it. –  Jan 08 '12 at 20:17
  • 3
    Note that using ```from django.conf import settings``` is critical if you want to be able to overwrite settings during unit testing as well. – Joris Oct 19 '15 at 11:46
  • 5
    Also note that all settings names **should be upper-case**. That is, if your have a var `my_var` in your settings then this will fail: `from django.conf import settings`... `settings.my_var`. But if you name it `MY_VAR` it will succeed! – nik_m Feb 09 '17 at 16:34