What's the difference between `from django.conf import settings` and `import settings` in a Django project

PythonDjangoDjango Settings

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Hope this helps.

Solution 2 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontzendermanView Question on Stackoverflow
Solution 1 - PythonjuliocesarView Answer on Stackoverflow
Solution 2 - PythonmansuetusView Answer on Stackoverflow