You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: LICENSE
+12Lines changed: 12 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -85,3 +85,15 @@ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
85
85
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
86
86
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
87
87
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88
+
89
+
90
+
python-decouple
91
+
The MIT License (MIT)
92
+
93
+
Copyright (c) 2017 Henrique Bastos <henrique at bastos dot net>
94
+
95
+
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:
96
+
97
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
98
+
99
+
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.
Copy file name to clipboardExpand all lines: README.md
+106-7Lines changed: 106 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,13 +26,18 @@ in production using [12-factor](http://12factor.net/) principles.
26
26
27
27
> Hey just wanted to let you know that since I've started writing 12-factor apps I've found python-dotenv to be invaluable for all my projects. It's super useful and “just works.” --Daniel Fridkin
28
28
29
+
Installation
30
+
============
31
+
32
+
pip install -U python-dotenv
33
+
29
34
Usages
30
35
======
31
36
32
37
The easiest and most common usage consists on calling `load_dotenv` when
33
38
the application starts, which will load environment variables from a
34
-
file named `.env` in the current directory or any of its parents or from
35
-
the path specificied; after that, you can just call the
39
+
file named `.env` in the current directory, any of its parents or from
40
+
the path specified; after that, you can just call the
36
41
environment-related method you need as provided by `os.getenv`.
37
42
38
43
`.env` looks like this:
@@ -42,6 +47,8 @@ environment-related method you need as provided by `os.getenv`.
42
47
REDIS_ADDRESS=localhost:6379
43
48
MEANING_OF_LIFE=42
44
49
MULTILINE_VAR="hello\nworld"
50
+
MULTILINE_VAR2="hello
51
+
world"
45
52
```
46
53
47
54
You can optionally prefix each line with the word `export`, which will
`os.getenv` works but it can be tricky as times as the returned value is always a string. dotenv provides it's own version of [`getenv`](#reading-envvars-in-your-application) that handle type casting like `bool`, `int`, etc.
107
+
99
108
`load_dotenv` do not override existing System environment variables. To
100
109
override, pass `override=True` to `load_dotenv()`.
101
110
@@ -139,11 +148,6 @@ Django
139
148
If you are using django you should add the above loader script at the
140
149
top of `wsgi.py` and `manage.py`.
141
150
142
-
Installation
143
-
============
144
-
145
-
pip install -U python-dotenv
146
-
147
151
iPython Support
148
152
---------------
149
153
@@ -254,6 +258,97 @@ commands like so
254
258
255
259
$ fab config:set,hello,world config:set,foo,bar config:set,fizz=buzz
256
260
261
+
262
+
Reading envvars in your application
263
+
==============================================
264
+
265
+
Envvars works, but since `os.environ` or `os.getenv` only returns strings, it’s tricky.
266
+
267
+
Let’s say you have an envvar `DEBUG=False`. If you run:
268
+
269
+
```
270
+
if os.environ['DEBUG']:
271
+
print True
272
+
else:
273
+
print False
274
+
```
275
+
276
+
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`.
277
+
278
+
python-dotenv provides a solution that doesn’t look like a workaround: `getenv('DEBUG', cast=bool)`.
By default, all values returned by `env` are strings, after all they are read from the envvars.
292
+
293
+
However, your Python code may expect some other value type, for example:
294
+
295
+
* Django’s DEBUG expects a boolean True or False.
296
+
* Django’s EMAIL_PORT expects an integer.
297
+
* Django’s ALLOWED_HOSTS expects a list of hostnames.
298
+
* Django’s SECURE_PROXY_SSL_HEADER expects a tuple with two elements, the name of the header to look for and the required value.
299
+
300
+
To meet this need, the `env` function accepts a `cast` argument which receives any callable, that will be used to transform the string value into something else.
301
+
302
+
Let’s see some examples for the above mentioned cases:
0 commit comments