Skip to main content

Let Django use settings from an arbitrary Python file instead of an importable module

Project description

https://img.shields.io/github/workflow/status/adamchainz/django-settings-file/CI/main?style=for-the-badge https://img.shields.io/pypi/v/django-settings-file.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Let Django use settings from an arbitrary Python file instead of an importable module.

As per James Pic’s idea posted on the django-developers mailing list.

Requirements

Python 3.6 to 3.9 supported.

Django 2.2 to 3.2 supported.


Are your tests slow? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.


Usage

  1. Install with python -m pip install django-settings-file.

  2. Edit your manage.py and wsgi.py to swap out Django’s default logic for setting DJANGO_SETTINGS_MODULE to instead do:

    import django_settings_file
    
    django_settings_file.setup()
  3. Add os.environ.setdefault('DJANGO_SETTINGS_FILE', '/path/to/default.py') before the setup() call, unless you can be sure DJANGO_SETTINGS_FILE will always be defined in your environment. You might need to figure out the path relative to your manage.py with some os.path manipulation.

  4. Run it! If DJANGO_SETTINGS_MODULE is defined, it will raise a DjangoSettingsFileError with a message about how only DJANGO_SETTINGS_FILE is allowed now. If DJANGO_SETTINGS_FILE is not defined, it will also fail with a DjangoSettingsFileError with 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 hacky module.

Caveats

  • If the Python file defined by DJANGO_SETTINGS_FILE tries 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 PYTHONPATH.

  • If you want your settings file to extend another one, it will probably find it easiest to import the 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 .py and .pyc files. In most cases this means a .pyc file will not be used for settings since it can’t be guaranteed to be there, slightly slowing down import time.

  • DJANGO_SETTINGS_MODULE and DJANGO_SETTINGS_FILE can’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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-settings-file-2.5.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

django_settings_file-2.5.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file django-settings-file-2.5.0.tar.gz.

File metadata

  • Download URL: django-settings-file-2.5.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for django-settings-file-2.5.0.tar.gz
Algorithm Hash digest
SHA256 2543d83639d61229f0662bd06e4c3a426b4d9a6caba39a21183c1f305e363923
MD5 556ec0938f2210dc85d338687ab59214
BLAKE2b-256 2739831a4af42e8af07694d40df63b4d04fedd2291583101c570fbcca99bf486

See more details on using hashes here.

File details

Details for the file django_settings_file-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: django_settings_file-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for django_settings_file-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4ed2601c862935a32f2af9c633f3b964358da7ad3cf1ab204694683f47a9152
MD5 36b93b71ee9c511a4be3134269f98c4c
BLAKE2b-256 f82d3825fe8837c9cbd24ee1287b06dcd14a3fc53878b0915fb6a55f7e705da1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page