All Projects → henriquebastos → Python Decouple

henriquebastos / Python Decouple

Licence: mit
Strict separation of config from code.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Python Decouple

climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-98.99%)
Mutual labels:  environment-variables, configuration-files
config-cpp
C++ Configuration management library inspired by the Viper package for golang.
Stars: ✭ 21 (-98.94%)
Mutual labels:  environment-variables, configuration-files
jsonargparse
Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
Stars: ✭ 168 (-91.52%)
Mutual labels:  environment-variables, configuration-files
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (-95.66%)
Mutual labels:  environment-variables, configuration-files
Phoenix gon
🔥 Phoenix variables in your JavaScript without headache.
Stars: ✭ 84 (-95.76%)
Mutual labels:  environment-variables
Variable Injector
Continuous Integration Tool for Swift Projects
Stars: ✭ 63 (-96.82%)
Mutual labels:  environment-variables
Dotenv Webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Stars: ✭ 1,022 (-48.44%)
Mutual labels:  environment-variables
Mconfig
MCONFIG is a lightweight Golang library for integrating configs files like (json, yml, toml) and environment variables into one config struct.
Stars: ✭ 28 (-98.59%)
Mutual labels:  environment-variables
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (-93.64%)
Mutual labels:  environment-variables
Fsconfig
FsConfig is a F# library for reading configuration data from environment variables and AppSettings with type safety.
Stars: ✭ 108 (-94.55%)
Mutual labels:  environment-variables
Env
Simple library to read environment variables and convert to simple types.
Stars: ✭ 73 (-96.32%)
Mutual labels:  environment-variables
Sweet
Official repository for Semantic Web for Earth and Environmental Terminology (SWEET) Ontologies
Stars: ✭ 69 (-96.52%)
Mutual labels:  environment-variables
React Env
Runtime environment variables for react apps.
Stars: ✭ 90 (-95.46%)
Mutual labels:  environment-variables
Properties
This library provides convinient way to work with properties. It can handle property-files on hard drive, in classpath or get values from system properties
Stars: ✭ 49 (-97.53%)
Mutual labels:  environment-variables
Cra Runtime Environment Variables
Guide: Runtime environment variables within Create React App
Stars: ✭ 111 (-94.4%)
Mutual labels:  environment-variables
Env Cmd
Setting environment variables from a file
Stars: ✭ 969 (-51.11%)
Mutual labels:  environment-variables
Env Providers
👷 Load Laravel service providers based on your application's environment.
Stars: ✭ 73 (-96.32%)
Mutual labels:  environment-variables
Startup
🔧 R package: startup - Friendly R Startup Configuration
Stars: ✭ 107 (-94.6%)
Mutual labels:  environment-variables
Dotenv Java
🗝️ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file
Stars: ✭ 72 (-96.37%)
Mutual labels:  environment-variables
Senv
Friends don't let friends leak secrets on their terminal window 🙈
Stars: ✭ 71 (-96.42%)
Mutual labels:  environment-variables

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:

  1. store parameters in ini or .env files;
  2. define comprehensive default values;
  3. properly convert values to the correct data type;
  4. 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.

Build Status Latest PyPI version

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:

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:

pip install python-decouple

Then use it on your settings.py.

  1. Import the config object:

    from decouple import config
  2. Retrieve the configuration parameters:

    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.

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:

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:

[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:

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 and dj-database-url.

# 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('EMAIL_PORT', default=25, cast=int)
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default='')
EMAIL_HOST_USER = config('EMAIL_HOST_USER', default='')
EMAIL_USE_TLS = config('EMAIL_USE_TLS', default=False, cast=bool)

# ...

Attention with undefined parameters

In the above example, all configuration parameters except SECRET_KEY = config('SECRET_KEY') have a default value in case it does not exist in the .env file.

If SECRET_KEY is not present in the .env, decouple will raise an UndefinedValueError.

This fail fast policy helps you avoid chasing misbehaviours when you eventually forget a parameter.

Overriding config files with environment variables

Sometimes you may want to change a parameter value without having to edit the .ini or .env files.

Since version 3.0, decouple respects the unix way. Therefore environment variables have precedence over config files.

To override a config parameter you can simply do:

DEBUG=True python manage.py

How does it work?

Decouple always searches for Options in this order:

  1. Environment variables;
  2. Repository: ini or .env file;
  3. Default argument passed to config.

There are 4 classes doing the magic:

  • Config

    Coordinates all the configuration retrieval.

  • RepositoryIni

    Can read values from os.environ and ini files, in that order.

    Note: Since version 3.0 decouple respects unix precedence of environment variables over config files.

  • RepositoryEnv

    Can read values from os.environ and .env files.

    Note: Since version 3.0 decouple respects unix precedence of environment variables over config files.

  • AutoConfig

    This is a lazy Config factory that detects which configuration repository you're using.

    It recursively searches up your configuration module path looking for a settings.ini or a .env file.

    Optionally, it accepts search_path argument to explicitly define where the search starts.

The config object is an instance of AutoConfig that instantiates a Config with the proper Repository on the first time it is used.

Understanding the CAST argument

By default, all values returned by decouple are strings, after all they are read from text files or the envvars.

However, your Python code may expect some other value type, for example:

  • Django's DEBUG expects a boolean True or False.
  • Django's EMAIL_PORT expects an integer.
  • Django's ALLOWED_HOSTS expects a list of hostnames.
  • Django's SECURE_PROXY_SSL_HEADER expects a tuple with two elements, the name of the header to look for and the required value.

To meet this need, the config function accepts a cast argument which receives any callable, that will be used to transform the string value into something else.

Let's see some examples for the above mentioned cases:

>>> os.environ['DEBUG'] = 'False'
>>> config('DEBUG', cast=bool)
False

>>> os.environ['EMAIL_PORT'] = '42'
>>> config('EMAIL_PORT', cast=int)
42

>>> os.environ['ALLOWED_HOSTS'] = '.localhost, .herokuapp.com'
>>> config('ALLOWED_HOSTS', cast=lambda v: [s.strip() for s in v.split(',')])
['.localhost', '.herokuapp.com']

>>> os.environ['SECURE_PROXY_SSL_HEADER'] = 'HTTP_X_FORWARDED_PROTO, https'
>>> config('SECURE_PROXY_SSL_HEADER', cast=Csv(post_process=tuple))
('HTTP_X_FORWARDED_PROTO', 'https')

As you can see, cast is very flexible. But the last example got a bit complex.

Built in Csv Helper

To address the complexity of the last example, Decouple comes with an extensible Csv helper.

Let's improve the last example:

>>> from decouple import Csv
>>> os.environ['ALLOWED_HOSTS'] = '.localhost, .herokuapp.com'
>>> config('ALLOWED_HOSTS', cast=Csv())
['.localhost', '.herokuapp.com']

You can also have a default value that must be a string to be processed by Csv.

>>> from decouple import Csv
>>> config('ALLOWED_HOSTS', default='127.0.0.1', cast=Csv())
['127.0.0.1']

You can also parametrize the Csv Helper to return other types of data.

>>> os.environ['LIST_OF_INTEGERS'] = '1,2,3,4,5'
>>> config('LIST_OF_INTEGERS', cast=Csv(int))
[1, 2, 3, 4, 5]

>>> os.environ['COMPLEX_STRING'] = '%virtual_env%\t *important stuff*\t   trailing spaces   '
>>> csv = Csv(cast=lambda s: s.upper(), delimiter='\t', strip=' %*')
>>> csv(os.environ['COMPLEX_STRING'])
['VIRTUAL_ENV', 'IMPORTANT STUFF', 'TRAILING SPACES']

By default Csv returns a list, but you can get a tuple or whatever you want using the post_process argument:

>>> os.environ['SECURE_PROXY_SSL_HEADER'] = 'HTTP_X_FORWARDED_PROTO, https'
>>> config('SECURE_PROXY_SSL_HEADER', cast=Csv(post_process=tuple))
('HTTP_X_FORWARDED_PROTO', 'https')

Built in Choices helper

Allows for cast and validation based on a list of choices. For example:

>>> from decouple import config, Choices
>>> os.environ['CONNECTION_TYPE'] = 'usb'
>>> config('CONNECTION_TYPE', cast=Choices(['eth', 'usb', 'bluetooth']))
'usb'

>>> os.environ['CONNECTION_TYPE'] = 'serial'
>>> config('CONNECTION_TYPE', cast=Choices(['eth', 'usb', 'bluetooth']))
Traceback (most recent call last):
 ...
ValueError: Value not in list: 'serial'; valid values are ['eth', 'usb', 'bluetooth']

You can also parametrize Choices helper to cast to another type:

>>> os.environ['SOME_NUMBER'] = '42'
>>> config('SOME_NUMBER', cast=Choices([7, 14, 42], cast=int))
42

You can also use a Django-like choices tuple:

>>> USB = 'usb'
>>> ETH = 'eth'
>>> BLUETOOTH = 'bluetooth'
>>>
>>> CONNECTION_OPTIONS = (
...        (USB, 'USB'),
...        (ETH, 'Ethernet'),
...        (BLUETOOTH, 'Bluetooth'),)
...
>>> os.environ['CONNECTION_TYPE'] = BLUETOOTH
>>> config('CONNECTION_TYPE', cast=Choices(choices=CONNECTION_OPTIONS))
'bluetooth'

Contribute

Your contribution is welcome.

Setup your development environment:

git clone [email protected]:henriquebastos/python-decouple.git
cd python-decouple
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
tox

Decouple supports both Python 2.7 and 3.6. Make sure you have both installed.

I use pyenv to manage multiple Python versions and I described my workspace setup on this article: The definitive guide to setup my Python workspace

You can submit pull requests and issues for discussion. However I only consider merging tested code.

License

The MIT License (MIT)

Copyright (c) 2017 Henrique Bastos <henrique at bastos dot net>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].