You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

479 lines
15 KiB

  1. Metadata-Version: 2.1
  2. Name: python-dotenv
  3. Version: 0.10.3
  4. Summary: Add .env support to your django/flask apps in development and deployments
  5. Home-page: http://github.com/theskumar/python-dotenv
  6. Author: Saurabh Kumar
  7. Author-email: me+github@saurabh-kumar.com
  8. License: UNKNOWN
  9. Keywords: environment variables,deployments,settings,env,dotenv,configurations,python
  10. Platform: UNKNOWN
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Programming Language :: Python
  13. Classifier: Programming Language :: Python :: 2.7
  14. Classifier: Programming Language :: Python :: 3.4
  15. Classifier: Programming Language :: Python :: 3.5
  16. Classifier: Programming Language :: Python :: 3.6
  17. Classifier: Programming Language :: Python :: 3.7
  18. Classifier: Programming Language :: Python :: Implementation :: PyPy
  19. Classifier: Intended Audience :: Developers
  20. Classifier: Intended Audience :: System Administrators
  21. Classifier: License :: OSI Approved :: BSD License
  22. Classifier: Operating System :: OS Independent
  23. Classifier: Topic :: System :: Systems Administration
  24. Classifier: Topic :: Utilities
  25. Classifier: Environment :: Web Environment
  26. Description-Content-Type: text/markdown
  27. Requires-Dist: typing ; python_version < "3.5"
  28. Provides-Extra: cli
  29. Requires-Dist: click (>=5.0) ; extra == 'cli'
  30. ```
  31. _______ .__ __. ____ ____
  32. | ____|| \ | | \ \ / /
  33. | |__ | \| | \ \/ /
  34. | __| | . ` | \ /
  35. __ | |____ | |\ | \ /
  36. (__)|_______||__| \__| \__/
  37. ```
  38. python-dotenv | [![Build Status](https://travis-ci.org/theskumar/python-dotenv.svg?branch=master)](https://travis-ci.org/theskumar/python-dotenv) [![Coverage Status](https://coveralls.io/repos/theskumar/python-dotenv/badge.svg?branch=master)](https://coveralls.io/r/theskumar/python-dotenv?branch=master) [![PyPI version](https://badge.fury.io/py/python-dotenv.svg)](http://badge.fury.io/py/python-dotenv) [![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/theskumar)
  39. ===============================================================================
  40. Reads the key,value pair from `.env` file and adds them to environment
  41. variable. It is great for managing app settings during development and
  42. in production using [12-factor](http://12factor.net/) principles.
  43. > Do one thing, do it well!
  44. - [Usages](#usages)
  45. - [Installation](#installation)
  46. - [Command-line interface](#command-line-interface)
  47. - [iPython Support](#ipython-support)
  48. - [Setting config on remote servers](#setting-config-on-remote-servers)
  49. - [Related Projects](#related-projects)
  50. - [Contributing](#contributing)
  51. - [Changelog](#changelog)
  52. > 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
  53. Usages
  54. ======
  55. The easiest and most common usage consists on calling `load_dotenv` when
  56. the application starts, which will load environment variables from a
  57. file named `.env` in the current directory or any of its parents or from
  58. the path specificied; after that, you can just call the
  59. environment-related method you need as provided by `os.getenv`.
  60. `.env` looks like this:
  61. ```shell
  62. # a comment and that will be ignored.
  63. REDIS_ADDRESS=localhost:6379
  64. MEANING_OF_LIFE=42
  65. MULTILINE_VAR="hello\nworld"
  66. ```
  67. You can optionally prefix each line with the word `export`, which is totally ignored by this library, but might allow you to [`source`](https://bash.cyberciti.biz/guide/Source_command) the file in bash.
  68. ```
  69. export S3_BUCKET=YOURS3BUCKET
  70. export SECRET_KEY=YOURSECRETKEYGOESHERE
  71. ```
  72. `.env` can interpolate variables using POSIX variable expansion,
  73. variables are replaced from the environment first or from other values
  74. in the `.env` file if the variable is not present in the environment.
  75. (`Note`: Default Value Expansion is not supported as of yet, see
  76. [\#30](https://github.com/theskumar/python-dotenv/pull/30#issuecomment-244036604).)
  77. ```shell
  78. CONFIG_PATH=${HOME}/.config/foo
  79. DOMAIN=example.org
  80. EMAIL=admin@${DOMAIN}
  81. ```
  82. Getting started
  83. ===============
  84. Assuming you have created the `.env` file along-side your settings
  85. module.
  86. .
  87. ├── .env
  88. └── settings.py
  89. Add the following code to your `settings.py`
  90. ```python
  91. # settings.py
  92. from dotenv import load_dotenv
  93. load_dotenv()
  94. # OR, the same with increased verbosity:
  95. load_dotenv(verbose=True)
  96. # OR, explicitly providing path to '.env'
  97. from pathlib import Path # python3 only
  98. env_path = Path('.') / '.env'
  99. load_dotenv(dotenv_path=env_path)
  100. ```
  101. At this point, parsed key/value from the .env file is now present as
  102. system environment variable and they can be conveniently accessed via
  103. `os.getenv()`
  104. ```python
  105. # settings.py
  106. import os
  107. SECRET_KEY = os.getenv("EMAIL")
  108. DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")
  109. ```
  110. `load_dotenv` do not override existing System environment variables. To
  111. override, pass `override=True` to `load_dotenv()`.
  112. `load_dotenv` also accepts `encoding` parameter to open the `.env` file. The default encoding is platform dependent (whatever `locale.getpreferredencoding()` returns), but any encoding supported by Python can be used. See the [codecs](https://docs.python.org/3/library/codecs.html#standard-encodings) module for the list of supported encodings.
  113. You can use `find_dotenv()` method that will try to find a `.env` file
  114. by (a) guessing where to start using `__file__` or the working directory
  115. -- allowing this to work in non-file contexts such as IPython notebooks
  116. and the REPL, and then (b) walking up the directory tree looking for the
  117. specified file -- called `.env` by default.
  118. ```python
  119. from dotenv import load_dotenv, find_dotenv
  120. load_dotenv(find_dotenv())
  121. ```
  122. In-memory filelikes
  123. -------------------
  124. It is possible to not rely on the filesystem to parse filelikes from
  125. other sources (e.g. from a network storage). `load_dotenv` and
  126. `dotenv_values` accepts a filelike `stream`. Just be sure to rewind it
  127. before passing.
  128. ```python
  129. >>> from io import StringIO # Python2: from StringIO import StringIO
  130. >>> from dotenv import dotenv_values
  131. >>> filelike = StringIO('SPAM=EGGS\n')
  132. >>> filelike.seek(0)
  133. >>> parsed = dotenv_values(stream=filelike)
  134. >>> parsed['SPAM']
  135. 'EGGS'
  136. ```
  137. The returned value is dictionary with key value pair.
  138. `dotenv_values` could be useful if you need to *consume* the envfile but
  139. not *apply* it directly into the system environment.
  140. Django
  141. ------
  142. If you are using django you should add the above loader script at the
  143. top of `wsgi.py` and `manage.py`.
  144. Installation
  145. ============
  146. pip install -U python-dotenv
  147. iPython Support
  148. ---------------
  149. You can use dotenv with iPython. You can either let the dotenv search
  150. for .env with %dotenv or provide the path to .env file explicitly, see
  151. below for usages.
  152. %load_ext dotenv
  153. # Use find_dotenv to locate the file
  154. %dotenv
  155. # Specify a particular file
  156. %dotenv relative/or/absolute/path/to/.env
  157. # Use '-o' to indicate override of existing variables
  158. %dotenv -o
  159. # Use '-v' to turn verbose mode on
  160. %dotenv -v
  161. Command-line interface
  162. ======================
  163. For commandline support, use the cli option during installation:
  164. pip install -U "python-dotenv[cli]"
  165. A cli interface `dotenv` is also included, which helps you manipulate
  166. the `.env` file without manually opening it. The same cli installed on
  167. remote machine combined with fabric (discussed later) will enable you to
  168. update your settings on remote server, handy isn't it!
  169. ```
  170. Usage: dotenv [OPTIONS] COMMAND [ARGS]...
  171. This script is used to set, get or unset values from a .env file.
  172. Options:
  173. -f, --file PATH Location of the .env file, defaults to .env
  174. file in current working directory.
  175. -q, --quote [always|never|auto]
  176. Whether to quote or not the variable values.
  177. Default mode is always. This does not affect
  178. parsing.
  179. --help Show this message and exit.
  180. Commands:
  181. get Retrive the value for the given key.
  182. list Display all the stored key/value.
  183. run Run command with environment variables from .env file present
  184. set Store the given key/value.
  185. unset Removes the given key.
  186. ```
  187. Setting config on remote servers
  188. --------------------------------
  189. We make use of excellent [Fabric](http://www.fabfile.org/) to acomplish
  190. this. Add a config task to your local fabfile, `dotenv_path` is the
  191. location of the absolute path of `.env` file on the remote server.
  192. ```python
  193. # fabfile.py
  194. import dotenv
  195. from fabric.api import task, run, env
  196. # absolute path to the location of .env on remote server.
  197. env.dotenv_path = '/opt/myapp/.env'
  198. @task
  199. def config(action=None, key=None, value=None):
  200. '''Manage project configuration via .env
  201. e.g: fab config:set,<key>,<value>
  202. fab config:get,<key>
  203. fab config:unset,<key>
  204. fab config:list
  205. '''
  206. run('touch %(dotenv_path)s' % env)
  207. command = dotenv.get_cli_string(env.dotenv_path, action, key, value)
  208. run(command)
  209. ```
  210. Usage is designed to mirror the heroku config api very closely.
  211. Get all your remote config info with `fab config`
  212. $ fab config
  213. foo="bar"
  214. Set remote config variables with `fab config:set,<key>,<value>`
  215. $ fab config:set,hello,world
  216. Get a single remote config variables with `fab config:get,<key>`
  217. $ fab config:get,hello
  218. Delete a remote config variables with `fab config:unset,<key>`
  219. $ fab config:unset,hello
  220. Thanks entirely to fabric and not one bit to this project, you can chain
  221. commands like so
  222. `fab config:set,<key1>,<value1> config:set,<key2>,<value2>`
  223. $ fab config:set,hello,world config:set,foo,bar config:set,fizz=buzz
  224. Related Projects
  225. ================
  226. - [Honcho](https://github.com/nickstenning/honcho) - For managing
  227. Procfile-based applications.
  228. - [django-dotenv](https://github.com/jpadilla/django-dotenv)
  229. - [django-environ](https://github.com/joke2k/django-environ)
  230. - [django-configuration](https://github.com/jezdez/django-configurations)
  231. - [dump-env](https://github.com/sobolevn/dump-env)
  232. - [environs](https://github.com/sloria/environs)
  233. Contributing
  234. ============
  235. All the contributions are welcome! Please open [an
  236. issue](https://github.com/theskumar/python-dotenv/issues/new) or send us
  237. a pull request.
  238. This project is currently maintained by [Saurabh Kumar](https://saurabh-kumar.com) and [Bertrand Bonnefoy-Claudet](https://github.com/bbc2) and would not
  239. have been possible without the support of these [awesome
  240. people](https://github.com/theskumar/python-dotenv/graphs/contributors).
  241. Executing the tests:
  242. $ pip install -r requirements.txt
  243. $ pip install -e .
  244. $ flake8
  245. $ pytest
  246. or with [tox](https://pypi.org/project/tox/) installed:
  247. $ tox
  248. Changelog
  249. =========
  250. Unreleased
  251. -----
  252. - Improve interactive mode detection ([@andrewsmith])([#183]).
  253. - Refactor parser to fix parsing inconsistencies ([@bbc2])([#170]).
  254. - Interpret escapes as control characters only in double-quoted strings.
  255. - Interpret `#` as start of comment only if preceded by whitespace.
  256. 0.10.2
  257. -----
  258. - Add type hints and expose them to users ([@qnighy])([#172])
  259. - `load_dotenv` and `dotenv_values` now accept an `encoding` parameter, defaults to `None`
  260. ([@theskumar])([@earlbread])([#161])
  261. - Fix `str`/`unicode` inconsistency in Python 2: values are always `str` now. ([@bbc2])([#121])
  262. - Fix Unicode error in Python 2, introduced in 0.10.0. ([@bbc2])([#176])
  263. 0.10.1
  264. -----
  265. - Fix parsing of variable without a value ([@asyncee])([@bbc2])([#158])
  266. 0.10.0
  267. -----
  268. - Add support for UTF-8 in unquoted values ([@bbc2])([#148])
  269. - Add support for trailing comments ([@bbc2])([#148])
  270. - Add backslashes support in values ([@bbc2])([#148])
  271. - Add support for newlines in values ([@bbc2])([#148])
  272. - Force environment variables to str with Python2 on Windows ([@greyli])
  273. - Drop Python 3.3 support ([@greyli])
  274. - Fix stderr/-out/-in redirection ([@venthur])
  275. 0.9.0
  276. -----
  277. - Add `--version` parameter to cli ([@venthur])
  278. - Enable loading from current directory ([@cjauvin])
  279. - Add 'dotenv run' command for calling arbitrary shell script with .env ([@venthur])
  280. 0.8.1
  281. -----
  282. - Add tests for docs ([@Flimm])
  283. - Make 'cli' support optional. Use `pip install python-dotenv[cli]`. ([@theskumar])
  284. 0.8.0
  285. -----
  286. - `set_key` and `unset_key` only modified the affected file instead of
  287. parsing and re-writing file, this causes comments and other file
  288. entact as it is.
  289. - Add support for `export` prefix in the line.
  290. - Internal refractoring ([@theskumar])
  291. - Allow `load_dotenv` and `dotenv_values` to work with `StringIO())` ([@alanjds])([@theskumar])([#78])
  292. 0.7.1
  293. -----
  294. - Remove hard dependency on iPython ([@theskumar])
  295. 0.7.0
  296. -----
  297. - Add support to override system environment variable via .env.
  298. ([@milonimrod](https://github.com/milonimrod))
  299. ([\#63](https://github.com/theskumar/python-dotenv/issues/63))
  300. - Disable ".env not found" warning by default
  301. ([@maxkoryukov](https://github.com/maxkoryukov))
  302. ([\#57](https://github.com/theskumar/python-dotenv/issues/57))
  303. 0.6.5
  304. -----
  305. - Add support for special characters `\`.
  306. ([@pjona](https://github.com/pjona))
  307. ([\#60](https://github.com/theskumar/python-dotenv/issues/60))
  308. 0.6.4
  309. -----
  310. - Fix issue with single quotes ([@Flimm])
  311. ([\#52](https://github.com/theskumar/python-dotenv/issues/52))
  312. 0.6.3
  313. -----
  314. - Handle unicode exception in setup.py
  315. ([\#46](https://github.com/theskumar/python-dotenv/issues/46))
  316. 0.6.2
  317. -----
  318. - Fix dotenv list command ([@ticosax](https://github.com/ticosax))
  319. - Add iPython Suport
  320. ([@tillahoffmann](https://github.com/tillahoffmann))
  321. 0.6.0
  322. -----
  323. - Drop support for Python 2.6
  324. - Handle escaped charaters and newlines in quoted values. (Thanks
  325. [@iameugenejo](https://github.com/iameugenejo))
  326. - Remove any spaces around unquoted key/value. (Thanks
  327. [@paulochf](https://github.com/paulochf))
  328. - Added POSIX variable expansion. (Thanks
  329. [@hugochinchilla](https://github.com/hugochinchilla))
  330. 0.5.1
  331. -----
  332. - Fix find\_dotenv - it now start search from the file where this
  333. function is called from.
  334. 0.5.0
  335. -----
  336. - Add `find_dotenv` method that will try to find a `.env` file.
  337. (Thanks [@isms](https://github.com/isms))
  338. 0.4.0
  339. -----
  340. - cli: Added `-q/--quote` option to control the behaviour of quotes
  341. around values in `.env`. (Thanks
  342. [@hugochinchilla](https://github.com/hugochinchilla)).
  343. - Improved test coverage.
  344. [#161]: https://github.com/theskumar/python-dotenv/issues/161
  345. [#78]: https://github.com/theskumar/python-dotenv/issues/78
  346. [#148]: https://github.com/theskumar/python-dotenv/issues/148
  347. [#158]: https://github.com/theskumar/python-dotenv/issues/158
  348. [#172]: https://github.com/theskumar/python-dotenv/issues/172
  349. [#121]: https://github.com/theskumar/python-dotenv/issues/121
  350. [#176]: https://github.com/theskumar/python-dotenv/issues/176
  351. [#170]: https://github.com/theskumar/python-dotenv/issues/170
  352. [#183]: https://github.com/theskumar/python-dotenv/issues/183
  353. [@andrewsmith]: https://github.com/andrewsmith
  354. [@asyncee]: https://github.com/asyncee
  355. [@greyli]: https://github.com/greyli
  356. [@venthur]: https://github.com/venthur
  357. [@Flimm]: https://github.com/Flimm
  358. [@theskumar]: https://github.com/theskumar
  359. [@alanjds]: https://github.com/alanjds
  360. [@cjauvin]: https://github.com/cjauvin
  361. [@bbc2]: https://github.com/bbc2
  362. [@qnighy]: https://github.com/qnighy
  363. [@earlbread]: https://github.com/earlbread