1
+ ========================================================
1
2
Python Decouple: Strict separation of settings from code
2
3
========================================================
3
4
@@ -22,8 +23,6 @@ for separating settings from code.
22
23
:target: https://pypi.python.org/pypi/python-decouple/
23
24
:alt: Latest PyPI version
24
25
25
-
26
-
27
26
.. contents :: Summary
28
27
29
28
@@ -42,6 +41,7 @@ The first 2 are *project settings* and the last 3 are *instance settings*.
42
41
43
42
You should be able to change *instance settings * without redeploying your app.
44
43
44
+
45
45
Why not just use environment variables?
46
46
---------------------------------------
47
47
@@ -61,6 +61,7 @@ Since it's a non-empty string, it will be evaluated as True.
61
61
62
62
*Decouple * provides a solution that doesn't look like a workaround: ``config('DEBUG', cast=bool) ``.
63
63
64
+
64
65
Usage
65
66
=====
66
67
@@ -88,8 +89,10 @@ Then use it on your ``settings.py``.
88
89
EMAIL_HOST = config(' EMAIL_HOST' , default = ' localhost' )
89
90
EMAIL_PORT = config(' EMAIL_PORT' , default = 25 , cast = int )
90
91
92
+
91
93
Encodings
92
94
---------
95
+
93
96
Decouple's default encoding is `UTF-8 `.
94
97
95
98
But you can specify your preferred encoding.
@@ -112,11 +115,13 @@ If you wish to fall back to your system's default encoding use:
112
115
config.encoding = locale.getpreferredencoding(False )
113
116
SECRET_KEY = config(' SECRET_KEY' )
114
117
118
+
115
119
Where is the settings data stored?
116
- -----------------------------------
120
+ ----------------------------------
117
121
118
122
*Decouple * supports both *.ini * and *.env * files.
119
123
124
+
120
125
Ini file
121
126
~~~~~~~~
122
127
@@ -134,6 +139,7 @@ Simply create a ``settings.ini`` next to your configuration module in the form:
134
139
135
140
*Note *: Since ``ConfigParser `` supports *string interpolation *, to represent the character ``% `` you need to escape it as ``%% ``.
136
141
142
+
137
143
Env file
138
144
~~~~~~~~
139
145
@@ -148,6 +154,7 @@ Simply create a ``.env`` text file in your repository's root directory in the fo
148
154
PERCENTILE=90%
149
155
#COMMENTED=42
150
156
157
+
151
158
Example: How do I use it with Django?
152
159
-------------------------------------
153
160
@@ -191,6 +198,7 @@ and `dj-database-url <https://pypi.python.org/pypi/dj-database-url/>`_.
191
198
192
199
# ...
193
200
201
+
194
202
Attention with *undefined * parameters
195
203
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196
204
@@ -201,6 +209,7 @@ If ``SECRET_KEY`` is not present in the ``.env``, *decouple* will raise an ``Und
201
209
202
210
This *fail fast * policy helps you avoid chasing misbehaviours when you eventually forget a parameter.
203
211
212
+
204
213
Overriding config files with environment variables
205
214
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206
215
@@ -297,6 +306,7 @@ Let's see some examples for the above mentioned cases:
297
306
298
307
As you can see, ``cast `` is very flexible. But the last example got a bit complex.
299
308
309
+
300
310
Built in Csv Helper
301
311
~~~~~~~~~~~~~~~~~~~
302
312
@@ -340,6 +350,7 @@ By default *Csv* returns a ``list``, but you can get a ``tuple`` or whatever you
340
350
>> > config(' SECURE_PROXY_SSL_HEADER' , cast = Csv(post_process = tuple ))
341
351
(' HTTP_X_FORWARDED_PROTO' , ' https' )
342
352
353
+
343
354
Built in Choices helper
344
355
~~~~~~~~~~~~~~~~~~~~~~~
345
356
@@ -383,56 +394,71 @@ You can also use a Django-like choices tuple:
383
394
>> > config(' CONNECTION_TYPE' , cast = Choices(choices = CONNECTION_OPTIONS ))
384
395
' bluetooth'
385
396
397
+
386
398
Frequently Asked Questions
387
399
==========================
388
400
401
+
389
402
1) How to specify the `.env ` path?
390
403
----------------------------------
391
404
392
- ```python
393
- import os
394
- from decouple import Config, RepositoryEnv
395
- config = Config(RepositoryEnv("path/to/.env"))
396
- ` ``
405
+ .. code-block :: python
406
+
407
+ import os
408
+ from decouple import Config, RepositoryEnv
409
+
410
+
411
+ config = Config(RepositoryEnv(" path/to/.env" ))
412
+
397
413
398
414
2) How to use python-decouple with Jupyter?
399
415
-------------------------------------------
400
416
401
- ```python
402
- import os
403
- from decouple import Config, RepositoryEnv
404
- config = Config(RepositoryEnv("path/to/.env"))
405
- ` ``
417
+ .. code-block :: python
418
+
419
+ import os
420
+ from decouple import Config, RepositoryEnv
421
+
422
+
423
+ config = Config(RepositoryEnv(" path/to/.env" ))
424
+
406
425
407
426
3) How to specify a file with another name instead of `.env `?
408
427
----------------------------------------------------------------
409
428
410
- ```python
411
- import os
412
- from decouple import Config, RepositoryEnv
413
- config = Config(RepositoryEnv("path/to/somefile-like-env"))
414
- ` ``
429
+ .. code-block :: python
430
+
431
+ import os
432
+ from decouple import Config, RepositoryEnv
433
+
434
+
435
+ config = Config(RepositoryEnv(" path/to/somefile-like-env" ))
436
+
415
437
416
438
4) How to define the path to my env file on a env var?
417
439
--------------------------------------------------------
418
440
419
- ```python
420
- import os
421
- from decouple import Config, RepositoryEnv
441
+ .. code-block :: python
442
+
443
+ import os
444
+ from decouple import Config, RepositoryEnv
445
+
446
+
447
+ DOTENV_FILE = os.environ.get(" DOTENV_FILE" , " .env" ) # only place using os.environ
448
+ config = Config(RepositoryEnv(DOTENV_FILE ))
422
449
423
- DOTENV_FILE = os.environ.get("DOTENV_FILE", ".env") # only place using os.environ
424
- config = Config(RepositoryEnv(DOTENV_FILE))
425
- ` ``
426
450
427
451
5) How can I have multiple *env * files working together?
428
452
--------------------------------------------------------
429
453
430
- ```python
431
- from collections import ChainMap
432
- from decouple import Config, RepositoryEnv
454
+ .. code-block :: python
455
+
456
+ from collections import ChainMap
457
+ from decouple import Config, RepositoryEnv
458
+
459
+
460
+ config = Config(ChainMap(RepositoryEnv(" .private.env" ), RepositoryEnv(" .env" )))
433
461
434
- config = Config(ChainMap(RepositoryEnv(".private.env"), RepositoryEnv(".env")))
435
- ` ``
436
462
437
463
Contribute
438
464
==========
0 commit comments