Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Strict separation of config from code.

License

NotificationsYou must be signed in to change notification settings

HBNetwork/python-decouple

Repository files navigation

Decouple helps you to organize your settings so that you canchange parameters without having to redeploy your app.

It also makes it easy for you to:

  1. store parameters inini or.env files;
  2. define comprehensive default values;
  3. properly convert values to the correct data type;
  4. haveonly one configuration module to rule all your instances.

It was originally designed for Django, but became an independent generic toolfor separating settings from code.

Build StatusLatest PyPI version

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 areproject settings and the last 3 areinstance settings.

You should be able to changeinstance settings without redeploying your app.

Envvars works, but sinceos.environ only returns strings, it's tricky.

Let's say you have anenvvarDEBUG=False. If you run:

ifos.environ['DEBUG']:printTrueelse:printFalse

It will printTrue, becauseos.environ['DEBUG'] returns thestring"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).

Install:

pip install python-decouple

Then use it on yoursettings.py.

  1. Import theconfig object:

    fromdecoupleimportconfig
  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)

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 changeits encoding right after import.

fromdecoupleimportconfigconfig.encoding='cp1251'SECRET_KEY=config('SECRET_KEY')

If you wish to fall back to your system's default encoding use:

importlocalefromdecoupleimportconfigconfig.encoding=locale.getpreferredencoding(False)SECRET_KEY=config('SECRET_KEY')

Decouple supports both.ini and.env files.

Simply create asettings.ini next to your configuration module in the form:

[settings]DEBUG=TrueTEMPLATE_DEBUG=%(DEBUG)sSECRET_KEY=ARANDOMSECRETKEYDATABASE_URL=mysql://myuser:mypassword@myhost/mydatabasePERCENTILE=90%%#COMMENTED=42

Note: SinceConfigParser supportsstring interpolation, to represent the character% you need to escape it as%%.

Simply create a.env text file in your repository's root directory in the form:

DEBUG=TrueTEMPLATE_DEBUG=TrueSECRET_KEY=ARANDOMSECRETKEYDATABASE_URL=mysql://myuser:mypassword@myhost/mydatabasePERCENTILE=90%#COMMENTED=42

Given that I have a.env file in my repository's root directory, here is a snippet of mysettings.py.

I also recommend usingpathlibanddj-database-url.

# coding: utf-8fromdecoupleimportconfigfromunipathimportPathfromdj_database_urlimportparseasdb_urlBASE_DIR=Path(__file__).parentDEBUG=config('DEBUG',default=False,cast=bool)TEMPLATE_DEBUG=DEBUGDATABASES= {'default':config('DATABASE_URL',default='sqlite:///'+BASE_DIR.child('db.sqlite3'),cast=db_url    )}TIME_ZONE='America/Sao_Paulo'USE_L10N=TrueUSE_TZ=TrueSECRET_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)# ...

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

IfSECRET_KEY is not present in the.env,decouple will raise anUndefinedValueError.

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

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

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

To override a config parameter you can simply do:

DEBUG=True python manage.py

Decouple always searches forOptions 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 fromos.environ and ini files, in that order.

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

  • RepositoryEnv

    Can read values fromos.environ and.env files.

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

  • AutoConfig

    This is alazyConfig factory that detects which configuration repository you're using.

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

    Optionally, it acceptssearch_path argument to explicitly definewhere the search starts.

Theconfig object is an instance ofAutoConfig that instantiates aConfig with the properRepositoryon the first time it is used.

By default, all values returned bydecouple arestrings, after all they areread fromtext files or theenvvars.

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

  • Django'sDEBUG expects a booleanTrue orFalse.
  • Django'sEMAIL_PORT expects aninteger.
  • Django'sALLOWED_HOSTS expects alist of hostnames.
  • Django'sSECURE_PROXY_SSL_HEADER expects atuple with two elements, the name of the header to look for and the required value.

To meet this need, theconfig function accepts acast argument whichreceives anycallable, that will be used totransform the string valueinto 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=lambdav: [s.strip()forsinv.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.

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

Let's improve the last example:

>>>fromdecoupleimportCsv>>>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.

>>>fromdecoupleimportCsv>>>config('ALLOWED_HOSTS',default='127.0.0.1',cast=Csv())['127.0.0.1']

You can also parametrize theCsv 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=lambdas:s.upper(),delimiter='\t',strip=' %*')>>>csv(os.environ['COMPLEX_STRING'])['VIRTUAL_ENV','IMPORTANT STUFF','TRAILING SPACES']

By defaultCsv returns alist, but you can get atuple or whatever you want using thepost_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')

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

>>>fromdecoupleimportconfig,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 (mostrecentcalllast): ...ValueError:Valuenotinlist:'serial';validvaluesare ['eth','usb','bluetooth']

You can also parametrizeChoices 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'
importosfromdecoupleimportConfig,RepositoryEnvconfig=Config(RepositoryEnv("path/to/.env"))
importosfromdecoupleimportConfig,RepositoryEnvconfig=Config(RepositoryEnv("path/to/.env"))
importosfromdecoupleimportConfig,RepositoryEnvconfig=Config(RepositoryEnv("path/to/somefile-like-env"))
importosfromdecoupleimportConfig,RepositoryEnvDOTENV_FILE=os.environ.get("DOTENV_FILE",".env")# only place using os.environconfig=Config(RepositoryEnv(DOTENV_FILE))
fromcollectionsimportChainMapfromdecoupleimportConfig,RepositoryEnvconfig=Config(ChainMap(RepositoryEnv(".private.env"),RepositoryEnv(".env")))

Your contribution is welcome.

Setup your development environment:

git clone git@github.com:henriquebastos/python-decouple.gitcd python-decouplepython -m venv .venvsource .venv/bin/activatepip install -r requirements.txttox

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

I usepyenv tomanage 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 onlyconsider merging tested code.

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 copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

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

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 INTHE SOFTWARE.


[8]ページ先頭

©2009-2025 Movatter.jp