Description
Python Decouple: Strict separation of settings from code ======================================================== *Decouple* helps you to organize your settings so that you can change parameters without having to redeploy your app. It also makes it easy for you to: #. store parameters in *ini* or *.env* files; #. define comprehensive default values; #. properly convert values to the correct data type; #. have **only one** configuration module to rule all your instances. It was originally designed for Django, but became an independent generic tool for separating settings from code. .. image:: https://img.shields.io/travis/henriquebastos/python-decouple.svg :target: https://travis-ci.org/henriquebastos/python-decouple :alt: Build Status .. image:: https://img.shields.io/pypi/v/python-decouple.svg :target: https://pypi.python.org/pypi/python-decouple/ :alt: Latest PyPI version .. contents:: Summary Why? ==== The settings files in web frameworks store many different kinds of parameters: * Locale and i18n; * Middlewares and Installed Apps; * Resource handles to the database, Memcached, and other backing services; * Credentials to external services such as Amazon S3 or Twitter; * Per-deploy values such as the canonical hostname for the instance. The first 2 are *project settings* and the last 3 are *instance settings*. You should be able to change *instance settings* without redeploying your app. Why not just use environment variables? --------------------------------------- *Envvars* works, but since ``os.environ`` only returns strings, it's tricky. Let's say you have an *envvar* ``DEBUG=False``. If you run: .. code-block:: python if os.environ['DEBUG']: print True else: print False It will print **True**, because ``os.environ['DEBUG']`` returns the **string** ``"False"``. Since it's a non-empty string, it will be evaluated as True. *Decouple* provides a solution that doesn't look like a workaround: ``config('DEBUG', cast=bool)``. Usage ===== Install: .. code-block:: console pip install python-decouple Then use it on your ``settings.py``. #. Import the ``config`` object: .. code-block:: python from decouple import config #. Retrieve the configuration parameters: .. code-block:: python SECRET_KEY = config('SECRET_KEY') DEBUG = config('DEBUG', default=False, cast=bool) EMAIL_HOST = config('EMAIL_HOST', default='localhost') EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int) Encodings --------- Decouple's default encoding is `UTF-8`. But you can specify your preferred encoding. Since `config` is lazy and only opens the configuration file when it's first needed, you have the chance to change its encoding right after import. .. code-block:: python from decouple import config config.encoding = 'cp1251' SECRET_KEY = config('SECRET_KEY') If you wish to fall back to your system's default encoding use: .. code-block:: python import locale from decouple import config config.encoding = locale.getpreferredencoding(False) SECRET_KEY = config('SECRET_KEY') Where is the settings data stored? ----------------------------------- *Decouple* supports both *.ini* and *.env* files. Ini file ~~~~~~~~ Simply create a ``settings.ini`` next to your configuration module in the form: .. code-block:: ini [settings] DEBUG=True TEMPLATE_DEBUG=%(DEBUG)s SECRET_KEY=ARANDOMSECRETKEY DATABASE_URL=mysql://myuser:mypassword@myhost/mydatabase PERCENTILE=90%% #COMMENTED=42 *Note*: Since ``ConfigParser`` supports *string interpolation*, to represent the character ``%`` you need to escape it as ``%%``. Env file ~~~~~~~~ Simply create a ``.env`` text file in your repository's root directory in the form: .. code-block:: console DEBUG=True TEMPLATE_DEBUG=True SECRET_KEY=ARANDOMSECRETKEY DATABASE_URL=mysql://myuser:mypassword@myhost/mydatabase PERCENTILE=90% #COMMENTED=42 Example: How do I use it with Django? ------------------------------------- Given that I have a ``.env`` file in my repository's root directory, here is a snippet of my ``settings.py``. I also recommend using `pathlib <https://docs.python.org/3/library/pathlib.html>`_ and `dj-database-url <https://pypi.python.org/pypi/dj-database-url/>`_. .. code-block:: python # coding: utf-8 from decouple import config from unipath import Path from dj_database_url import parse as db_url BASE_DIR = Path(__file__).parent DEBUG = config('DEBUG', default=False, cast=bool) TEMPLATE_DEBUG = DEBUG DATABASES = { 'default': config( 'DATABASE_URL', default='sqlite:///' + BASE_DIR.child('db.sqlite3'), cast=db_url ) } TIME_ZONE = 'America/Sao_Paulo' USE_L10N = True USE_TZ = True SECRET_KEY = config('SECRET_KEY') EMAIL_HOST = config('EMAIL_HOST', default='localhost') EMAIL_PORT = config
Release History
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 3.8 | Imported from PyPI (3.8) | Low | 4/21/2026 |
| v3.8 | ## What's Changed * Raise KeyError when key is not found in ini repositories by @b0o in https://github.com/HBNetwork/python-decouple/pull/153 * Fix infinite recursion on Windows by @ibachar-es in https://github.com/HBNetwork/python-decouple/pull/137 ## New Contributors * @b0o made their first contribution in https://github.com/HBNetwork/python-decouple/pull/153 * @ibachar-es made their first contribution in https://github.com/HBNetwork/python-decouple/pull/137 **Full Changelog**: https | Low | 3/1/2023 |
| v3.7 | ## What's Changed * Fix `Csv` cast hanging with `default=None`, now returning an empty list. (#149) ## New Contributors * @iurisilvio made their first contribution in https://github.com/henriquebastos/python-decouple/pull/149 **Full Changelog**: https://github.com/henriquebastos/python-decouple/compare/v3.6...v3.7 | Low | 1/9/2023 |
| v3.6 | ## What's Changed * Update Changelog to reflect latest versions by @stevejalim in https://github.com/henriquebastos/python-decouple/pull/129 * fix: replace strtobool for local function by @ZandorSabino in https://github.com/henriquebastos/python-decouple/pull/128 ## New Contributors * @stevejalim made their first contribution in https://github.com/henriquebastos/python-decouple/pull/129 * @ZandorSabino made their first contribution in https://github.com/henriquebastos/python-decouple/pull | Low | 2/2/2022 |
| v3.5 | Release v3.5 | Low | 9/30/2021 |
| v3.4 | Release v3.4 | Low | 1/5/2021 |
