DESCRIPTION.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. ::
  2. _______ .__ __. ____ ____
  3. | ____|| \ | | \ \ / /
  4. | |__ | \| | \ \/ /
  5. | __| | . ` | \ /
  6. __ | |____ | |\ | \ /
  7. (__)|_______||__| \__| \__/
  8. python-dotenv | |Build Status| |Coverage Status| |PyPI version| |PyPI|
  9. ======================================================================
  10. Reads the key,value pair from ``.env`` and adds them to environment
  11. variable. It is great of managing app settings during development and in
  12. production using `12-factor <http://12factor.net/>`__ principles.
  13. Do one thing, do it well!
  14. - `Usages <#usages>`__
  15. - `Installation <#installation>`__
  16. - `Command-line interface <#command-line-interface>`__
  17. - `iPython Support <#ipython-support>`__
  18. - `Setting config on remote servers <#setting-config-on-remote-servers>`__
  19. - `Related Projects <#releated-projects>`__
  20. - `Contributing <#contributing>`__
  21. - `Changelog <#changelog>`__
  22. Usages
  23. ======
  24. Assuming you have created the ``.env`` file along-side your settings
  25. module.
  26. ::
  27. .
  28. ├── .env
  29. └── settings.py
  30. Add the following code to your ``settings.py``
  31. .. code:: python
  32. # settings.py
  33. from os.path import join, dirname
  34. from dotenv import load_dotenv
  35. dotenv_path = join(dirname(__file__), '.env')
  36. load_dotenv(dotenv_path)
  37. # OR, the same with increased verbosity:
  38. load_dotenv(dotenv_path, verbose=True)
  39. Alternatively, you can use ``find_dotenv()`` method that will try to find a
  40. ``.env`` file by (a) guessing where to start using ``__file__`` or the working
  41. directory -- allowing this to work in non-file contexts such as IPython notebooks
  42. and the REPL, and then (b) walking up the directory tree looking for the
  43. specified file -- called ``.env`` by default.
  44. .. code:: python
  45. from dotenv import load_dotenv, find_dotenv
  46. load_dotenv(find_dotenv())
  47. You can also set _load_dotenv_ to override existing variables:
  48. .. code:: python
  49. from dotenv import load_dotenv, find_dotenv
  50. load_dotenv(find_dotenv(), override=True)
  51. Now, you can access the variables either from system environment
  52. variable or loaded from ``.env`` file. **System environment variables
  53. gets higher precedence** and it's advised not to include it in version control.
  54. .. code:: python
  55. # settings.py
  56. SECRET_KEY = os.environ.get("SECRET_KEY")
  57. DATABASE_PASSWORD = os.environ.get("DATABASE_PASSWORD")
  58. ``.env`` is a simple text file. With each environment variables listed
  59. per line, in the format of ``KEY="Value"``, lines starting with `#` is
  60. ignored.
  61. .. code:: shell
  62. SOME_VAR=someval
  63. # I am a comment and that is OK
  64. FOO="BAR"
  65. ``.env`` can interpolate variables using POSIX variable expansion, variables
  66. are replaced from the environment first or from other values in the ``.env``
  67. file if the variable is not present in the environment. (``Note``: Default Value
  68. Expansion is not supported as of yet, see `#30 <https://github.com/theskumar/python-dotenv/pull/30#issuecomment-244036604>`__.)
  69. .. code:: shell
  70. CONFIG_PATH=${HOME}/.config/foo
  71. DOMAIN=example.org
  72. EMAIL=admin@${DOMAIN}
  73. Django
  74. ------
  75. If you are using django you should add the above loader script at the
  76. top of ``wsgi.py`` and ``manage.py``.
  77. Installation
  78. ============
  79. ::
  80. pip install -U python-dotenv
  81. Command-line interface
  82. ======================
  83. A cli interface ``dotenv`` is also included, which helps you manipulate
  84. the ``.env`` file without manually opening it. The same cli installed on
  85. remote machine combined with fabric (discussed later) will enable you to
  86. update your settings on remote server, handy isn't it!
  87. ::
  88. Usage: dotenv [OPTIONS] COMMAND [ARGS]...
  89. This script is used to set, get or unset values from a .env file.
  90. Options:
  91. -f, --file PATH Location of the .env file, defaults to .env
  92. file in current working directory.
  93. -q, --quote [always|never|auto]
  94. Whether to quote or not the variable values.
  95. Default mode is always. This does not affect
  96. parsing.
  97. --help Show this message and exit.
  98. Commands:
  99. get Retrive the value for the given key.
  100. list Display all the stored key/value.
  101. set Store the given key/value.
  102. unset Removes the given key.
  103. iPython Support
  104. ---------------
  105. 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.
  106. ::
  107. %load_ext dotenv
  108. # Use find_dotenv to locate the file
  109. %dotenv
  110. # Specify a particular file
  111. %dotenv relative/or/absolute/path/to/.env
  112. # Use _-o_ to indicate override of existing variables
  113. %dotenv -o
  114. # Use _-v_ to turn verbose mode on
  115. %dotenv -v
  116. Setting config on remote servers
  117. --------------------------------
  118. We make use of excellent `Fabric <http://www.fabfile.org/>`__ to
  119. acomplish this. Add a config task to your local fabfile, ``dotenv_path``
  120. is the location of the absolute path of ``.env`` file on the remote
  121. server.
  122. .. code:: python
  123. # fabfile.py
  124. import dotenv
  125. from fabric.api import task, run, env
  126. # absolute path to the location of .env on remote server.
  127. env.dotenv_path = '/opt/myapp/.env'
  128. @task
  129. def config(action=None, key=None, value=None):
  130. '''Manage project configuration via .env
  131. e.g: fab config:set,<key>,<value>
  132. fab config:get,<key>
  133. fab config:unset,<key>
  134. fab config:list
  135. '''
  136. run('touch %(dotenv_path)s' % env)
  137. command = dotenv.get_cli_string(env.dotenv_path, action, key, value)
  138. run(command)
  139. Usage is designed to mirror the heroku config api very closely.
  140. Get all your remote config info with ``fab config``
  141. ::
  142. $ fab config
  143. Set remote config variables with ``fab config:set,<key>,<value>``
  144. ::
  145. $ fab config:set,hello,world
  146. Get a single remote config variables with ``fab config:get,<key>``
  147. ::
  148. $ fab config:get,hello
  149. Delete a remote config variables with ``fab config:unset,<key>``
  150. ::
  151. $ fab config:unset,hello
  152. Thanks entirely to fabric and not one bit to this project, you can chain
  153. commands like so ``fab config:set,<key1>,<value1> config:set,<key2>,<value2>``
  154. ::
  155. $ fab config:set,hello,world config:set,foo,bar config:set,fizz=buzz
  156. Related Projects
  157. =================
  158. - `Honcho <https://github.com/nickstenning/honcho>`__ - For managing
  159. Procfile-based applications.
  160. - `django-dotenv <https://github.com/jpadilla/django-dotenv>`__
  161. - `django-environ <https://github.com/joke2k/django-environ>`__
  162. - `django-configuration <https://github.com/jezdez/django-configurations>`__
  163. Contributing
  164. ============
  165. All the contributions are welcome! Please open `an
  166. issue <https://github.com/theskumar/python-dotenv/issues/new>`__ or send
  167. us a pull request.
  168. This project is currently maintained by `Saurabh Kumar`_ and
  169. would not have been possible without the support of these `awesome people <https://github.com/theskumar/python-dotenv/graphs/contributors>`__.
  170. Executing the tests:
  171. ::
  172. $ flake8
  173. $ pytest
  174. Changelog
  175. =========
  176. 0.7.1
  177. ----
  178. - Remove hard dependency on iPython (`@theskumar`_)
  179. 0.7.0
  180. ----
  181. - Add support to override system environment variable via .env. (`@milonimrod`_) (`#63`_)
  182. - Disable ".env not found" warning by default (`@maxkoryukov`_) (`#57`_)
  183. 0.6.5
  184. ----
  185. - Add support for special characters `\` (`@pjona`_) (`#60`_)
  186. 0.6.4
  187. ----
  188. - Fix issue with single quotes (`@Flimm`_) (`#52`_)
  189. 0.6.3
  190. ----
  191. - Handle unicode exception in setup.py (`#46`_)
  192. 0.6.2
  193. ----
  194. - Fix `dotenv list` command (`@ticosax`_)
  195. - Add iPython Suport (`@tillahoffmann`_)
  196. 0.6.0
  197. -----
  198. - Drop support for Python 2.6
  199. - Handle escaped charaters and newlines in quoted values. (Thanks `@iameugenejo`_)
  200. - Remove any spaces around unquoted key/value. (Thanks `@paulochf`_)
  201. - Added POSIX variable expansion. (Thanks `@hugochinchilla`_)
  202. 0.5.1
  203. -----
  204. - Fix `find_dotenv` - it now start search from the file where this function is called from.
  205. 0.5.0
  206. -----
  207. - Add ``find_dotenv`` method that will try to find a ``.env`` file. (Thanks `@isms`_)
  208. 0.4.0
  209. -----
  210. - cli: Added ``-q/--quote`` option to control the behaviour of quotes around values in ``.env``. (Thanks `@hugochinchilla`_).
  211. - Improved test coverage.
  212. .. _@maxkoryukov: https://github.com/milonimrod
  213. .. _@maxkoryukov: https://github.com/maxkoryukov
  214. .. _@pjona: https://github.com/pjona
  215. .. _@Flimm: https://github.com/Flimm
  216. .. _@ticosax: https://github.com/ticosax
  217. .. _@tillahoffmann: https://github.com/tillahoffmann
  218. .. _@hugochinchilla: https://github.com/hugochinchilla
  219. .. _@isms: https://github.com/isms
  220. .. _@iameugenejo: https://github.com/iameugenejo
  221. .. _@paulochf: https://github.com/paulochf
  222. .. _@paulochf: https://github.com/theskumar
  223. .. _#63: https://github.com/theskumar/python-dotenv/issues/63
  224. .. _#60: https://github.com/theskumar/python-dotenv/issues/60
  225. .. _#57: https://github.com/theskumar/python-dotenv/issues/57
  226. .. _#52: https://github.com/theskumar/python-dotenv/issues/52
  227. .. _#46: https://github.com/theskumar/python-dotenv/issues/46
  228. .. Saurabh Kumar: https://saurabh-kumar.com
  229. .. |Build Status| image:: https://travis-ci.org/theskumar/python-dotenv.svg?branch=master
  230. :target: https://travis-ci.org/theskumar/python-dotenv
  231. .. |Coverage Status| image:: https://coveralls.io/repos/theskumar/python-dotenv/badge.svg?branch=master
  232. :target: https://coveralls.io/r/theskumar/python-dotenv?branch=master
  233. .. |PyPI version| image:: https://badge.fury.io/py/python-dotenv.svg
  234. :target: http://badge.fury.io/py/python-dotenv
  235. .. |PyPI| image:: https://img.shields.io/pypi/dm/python-dotenv.svg
  236. :target: http://badge.fury.io/py/python-dotenv