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
Allow load_dotenv and dotenv_values to work with StringIO objects (theskumar#98)
* Allow `load_dotenv` and `dotenv_values` to work with StringIO objects
* make flake8 happy
* Fix for python 2.7
* Try fixing unicode issue in python2
* python 2.7 related fixes
* Update docs
* Doc update
Copy file name to clipboardExpand all lines: README.rst
+99-83Lines changed: 99 additions & 83 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,38 @@ production using `12-factor <http://12factor.net/>`__ principles.
28
28
29
29
Usages
30
30
======
31
+
The easiest and most common usage consists on calling ``load_dotenv`` when the
32
+
application starts, which will load environment variables from a file named
33
+
``.env`` in the current directory or any of its parents or from the path specificied;
34
+
after that, you can just call the environment-related method you need as
35
+
provided by ``os.getenv``.
36
+
37
+
``.env`` looks like this:
38
+
39
+
.. code:: shell
40
+
41
+
# a comment and that will be ignored.
42
+
REDIS_ADDRESS=localhost:6379
43
+
MEANING_OF_LIFE=42
44
+
MULTILINE_VAR="hello\nworld"
45
+
46
+
You can optionally prefix each line with the word ``export``, which will
47
+
conveniently allow you to source the whole file on your shell.
48
+
49
+
``.env`` can interpolate variables using POSIX variable expansion, variables
50
+
are replaced from the environment first or from other values in the ``.env``
51
+
file if the variable is not present in the environment. (``Note``: Default Value
52
+
Expansion is not supported as of yet, see `#30 <https://github.com/theskumar/python-dotenv/pull/30#issuecomment-244036604>`__.)
53
+
54
+
.. code:: shell
55
+
56
+
CONFIG_PATH=${HOME}/.config/foo
57
+
DOMAIN=example.org
58
+
EMAIL=admin@${DOMAIN}
59
+
60
+
61
+
Getting started
62
+
================
31
63
32
64
Assuming you have created the ``.env`` file along-side your settings
33
65
module.
@@ -43,66 +75,64 @@ Add the following code to your ``settings.py``
43
75
.. code:: python
44
76
45
77
# settings.py
46
-
from os.path import join, dirname
47
78
from dotenv import load_dotenv
48
-
49
-
dotenv_path = join(dirname(__file__), '.env')
50
-
load_dotenv(dotenv_path)
79
+
load_dotenv()
51
80
52
81
# OR, the same with increased verbosity:
53
-
load_dotenv(dotenv_path, verbose=True)
54
-
55
-
Alternatively, you can use ``find_dotenv()`` method that will try to find a
56
-
``.env`` file by (a) guessing where to start using ``__file__`` or the working
57
-
directory -- allowing this to work in non-file contexts such as IPython notebooks
58
-
and the REPL, and then (b) walking up the directory tree looking for the
59
-
specified file -- called ``.env`` by default.
82
+
load_dotenv(verbose=True)
60
83
61
-
.. code:: python
84
+
# OR, explicitly providing path to '.env'
85
+
from pathlib import Path # python3 only
86
+
env_path = Path('.') /'.env'
87
+
load_dotenv(dotenv_path=env_path)
62
88
63
-
from dotenv import load_dotenv, find_dotenv
64
-
load_dotenv(find_dotenv())
65
89
66
-
You can also set ``load_dotenv`` to override existing variables:
90
+
At this point, parsed key/value from the `.env` file is now present as system
91
+
environment variable and they can be conveniently accessed via ``os.getenv()``
67
92
68
93
.. code:: python
69
94
70
-
from dotenv import load_dotenv, find_dotenv
71
-
load_dotenv(find_dotenv(), override=True)
95
+
# settings.py
96
+
import os
97
+
SECRET_KEY= os.getenv("EMAIL")
98
+
DATABASE_PASSWORD= os.getenv("DATABASE_PASSWORD")
99
+
72
100
73
-
Now, you can access the variables either from system environment
74
-
variable or loaded from ``.env`` file. **System environment variables
75
-
gets higher precedence** and it's advised not to include it in version control.
101
+
``load_dotenv`` do not override existing System environment variables, but
102
+
you can do so if you want by passing ``override=True`` to ``load_dotenv()``.
103
+
104
+
You can use ``find_dotenv()`` method that will try to find a ``.env`` file by
105
+
(a) guessing where to start using ``__file__`` or the working directory -- allowing this to work in non-file contexts such as IPython notebooks and the REPL, and then
106
+
(b) walking up the directory tree looking for the specified file -- called ``.env`` by default.
``.env`` is a simple text file. With each environment variables listed
86
-
per line, in the format of ``KEY="Value"``, lines starting with `#` is
87
-
ignored.
117
+
It is possible to not rely on the filesystem to parse filelikes from other sources
118
+
(e.g. from a network storage). ``load_dotenv`` and ``dotenv_values`` accepts a filelike ``stream``.
119
+
Just be sure to rewind it before passing.
88
120
89
-
.. code:: shell
121
+
.. code:: python
90
122
91
-
SOME_VAR=someval
92
-
# I am a comment and that is OK
93
-
FOO="BAR"
94
-
MULTILINE_VAR="hello\nworld"
123
+
>>>from io import StringIO # Python2: from StringIO import StringIO
124
+
>>>from dotenv import dotenv_values
125
+
>>> filelike = StringIO('SPAM=EGSS\n')
126
+
>>> filelike.seek(0)
127
+
>>> parsed = dotenv_values(stream=filelike)
128
+
>>> parsed['SPAM']
129
+
'EGSS'
95
130
96
-
``.env`` can interpolate variables using POSIX variable expansion, variables
97
-
are replaced from the environment first or from other values in the ``.env``
98
-
file if the variable is not present in the environment. (``Note``: Default Value
99
-
Expansion is not supported as of yet, see `#30 <https://github.com/theskumar/python-dotenv/pull/30#issuecomment-244036604>`__.)
100
131
101
-
.. code:: shell
132
+
The returned value is dictionary with key value pair.
102
133
103
-
CONFIG_PATH=${HOME}/.config/foo
104
-
DOMAIN=example.org
105
-
EMAIL=admin@${DOMAIN}
134
+
``dotenv_values`` could be useful if you need to *consume* the envfile but not *apply* it
135
+
directly into the system environment.
106
136
107
137
108
138
Django
@@ -112,42 +142,39 @@ If you are using django you should add the above loader script at the
112
142
top of ``wsgi.py`` and ``manage.py``.
113
143
114
144
115
-
In-memory filelikes
116
-
-------------------
145
+
Installation
146
+
============
117
147
118
-
Is possible to not rely on the filesystem to parse filelikes from other sources
119
-
(e.g. from a network storage). ``parse_dotenv`` accepts a filelike `stream`.
120
-
Just be sure to rewind it before passing.
148
+
::
121
149
122
-
.. code:: python
150
+
pip install -U python-dotenv
123
151
124
-
from io import StringIO # Python2: from StringIO import StringIO
125
-
from dotenv.main import parse_dotenv
126
-
filelike = StringIO('SPAM=EGSS\n')
127
-
filelike.seek(0)
128
-
parsed = parse_dotenv(stream=filelike)
129
152
130
-
The returned lazy generator yields ``(key,value)`` tuples.
131
-
To ease the consumption, is suggested to unpack it into a dictionary.
153
+
iPython Support
154
+
---------------
132
155
133
-
.. code:: python
156
+
You can use dotenv with iPython. You can either let the dotenv search for .env with `%dotenv` or provide the path to .env file explicitly, see below for usages.
134
157
135
-
os_env_like =dict(iter(parsed))
136
-
assert os_env_like['SPAM'] =='EGGS'
158
+
::
137
159
138
-
This could be useful if you need to *consume* the envfile but not *apply* it
139
-
directly into the system environment.
160
+
%load_ext dotenv
140
161
162
+
# Use find_dotenv to locate the file
163
+
%dotenv
141
164
142
-
Installation
143
-
============
165
+
# Specify a particular file
166
+
%dotenv relative/or/absolute/path/to/.env
167
+
168
+
# Use _-o_ to indicate override of existing variables
169
+
%dotenv -o
170
+
171
+
# Use _-v_ to turn verbose mode on
172
+
%dotenv -v
144
173
145
-
::
146
174
147
-
pip install -U python-dotenv
148
175
149
176
Command-line interface
150
-
======================
177
+
=================
151
178
152
179
A cli interface ``dotenv`` is also included, which helps you manipulate
153
180
the ``.env`` file without manually opening it. The same cli installed on
@@ -175,27 +202,6 @@ update your settings on remote server, handy isn't it!
175
202
set Store the given key/value.
176
203
unset Removes the given key.
177
204
178
-
iPython Support
179
-
---------------
180
-
181
-
You can use dotenv with iPython. You can either let the dotenv search for .env with `%dotenv` or provide the path to .env file explicitly, see below for usages.
182
-
183
-
::
184
-
185
-
%load_ext dotenv
186
-
187
-
# Use find_dotenv to locate the file
188
-
%dotenv
189
-
190
-
# Specify a particular file
191
-
%dotenv relative/or/absolute/path/to/.env
192
-
193
-
# Use _-o_ to indicate override of existing variables
194
-
%dotenv -o
195
-
196
-
# Use _-v_ to turn verbose mode on
197
-
%dotenv -v
198
-
199
205
200
206
Setting config on remote servers
201
207
--------------------------------
@@ -235,6 +241,7 @@ Get all your remote config info with ``fab config``
235
241
::
236
242
237
243
$ fab config
244
+
foo="bar"
238
245
239
246
Set remote config variables with ``fab config:set,<key>,<value>``
240
247
@@ -292,6 +299,13 @@ Executing the tests:
292
299
Changelog
293
300
=========
294
301
302
+
0.8.0
303
+
----------------------------
304
+
- ``set_key`` and ``unset_key`` only modified the affected file instead of parsing and re-writing file, this causes comments and other file entact as it is.
305
+
- Add support for ``export `` prefix in the line.
306
+
- Internal refractoring (`@theskumar`_)
307
+
- Allow ``load_dotenv`` and ``dotenv_values`` to work with ``StringIO())`` (`@alanjds`_)(`@theskumar`_) (`#78`_)
308
+
295
309
0.7.1
296
310
----------------------------
297
311
@@ -340,6 +354,7 @@ Changelog
340
354
- cli: Added ``-q/--quote`` option to control the behaviour of quotes around values in ``.env``. (Thanks `@hugochinchilla`_).
0 commit comments