I stopped maintaining this package as it has never been popular. If you want to use the functionality I recommend copying the small amount of code into your project.
Let Django use settings from an arbitrary Python file instead of an importable module.
Python 3.7 to 3.11 supported.
Django 3.2 to 4.1 supported.
Want to work smarter and faster? Check out my book Boost Your Django DX which covers many ways to improve your development experience.
python -m pip install django-settings-file.
wsgi.pyto swap out Django's default logic for setting
DJANGO_SETTINGS_MODULEto instead do:
import django_settings_file django_settings_file.setup()
os.environ.setdefault('DJANGO_SETTINGS_FILE', '/path/to/default.py')before the
setup()call, unless you can be sure
DJANGO_SETTINGS_FILEwill always be defined in your environment. You might need to figure out the path relative to your
Run it! If
DJANGO_SETTINGS_MODULEis defined, it will raise a
DjangoSettingsFileErrorwith a message about how only
DJANGO_SETTINGS_FILEis allowed now. If
DJANGO_SETTINGS_FILEis not defined, it will also fail with a
DjangoSettingsFileErrorwith a message about defining it. Otherwise Django should start with the settings!
How it works
django-settings-file imports the contents of the specified file using the import machinery available on your Python
version (different logic for 2 and 3) and copies it contents into its own module, which Django sees as the settings
file defined via the traditional
DJANGO_SETTINGS_MODULE. Nothing about Django is really touched, it's just a
- If the Python file defined by
DJANGO_SETTINGS_FILEtries to do any imports, the directory the file is in will not be on
PYTHONPATH, so the author of the settings file might get some surprises.
- Additionally, you might experience other problems from loading a file whilst it's not on
- If you want your settings file to extend another one, it will probably find it easiest to
importthe base one from a location on
PYTHONPATH, otherwise it too will have to do use the same import 'hacks' to load the default settings.
- File paths are not portable between operating systems, so you may need logic to support both Unix and Windows at once.
- File paths are not portable between
.pycfiles. In most cases this means a
.pycfile will not be used for settings since it can't be guaranteed to be there, slightly slowing down import time.
DJANGO_SETTINGS_FILEcan't both be used by the same project, since the module is required for the file-based logic. You might be able to allow them both with extra logic, pull requests accepted.
¯\_(ツ)_/¯- this is kind of unknown territory, this library has not been tested in any real project, just with the example project in the test folder.