|
| 1 | +<pre> |
| 2 | + _______ .__ __. ____ ____ |
| 3 | + | ____|| \ | | \ \ / / |
| 4 | + | |__ | \| | \ \/ / |
| 5 | + | __| | . ` | \ / |
| 6 | + __ | |____ | |\ | \ / |
| 7 | +(__)|_______||__| \__| \__/ |
| 8 | +</pre> |
1 | 9 | # python-dotenv |
2 | 10 |
|
3 | 11 | [](https://travis-ci.org/theskumar/python-dotenv) [](https://coveralls.io/r/theskumar/python-dotenv?branch=master) [](http://badge.fury.io/py/python-dotenv) []() |
4 | 12 |
|
5 | | -# Features |
| 13 | +Reads the key,value pair from `.env` and adds them to environment variable. It is great of managing app settings during development and in production using [12-factor] principles. |
6 | 14 |
|
7 | | -The original work is based on [django-dotenv](https://github.com/jacobian/django-dotenv) by jacobian. |
| 15 | +> Do one thing, do it well! |
8 | 16 |
|
9 | | -- read values from .env file and loads them as environment variable. |
10 | | -- use it any python project not just django. |
11 | | -- commandline interface to read/write `.env` file on your local and remote servers. |
12 | | -- python 2 and 3 support |
| 17 | +<!-- MarkdownTOC --> |
13 | 18 |
|
| 19 | +- [Usages](#usages) |
| 20 | +- [Installation](#installation) |
| 21 | +- [Command-line interface](#command-line-interface) |
| 22 | + - [Setting config on remote servers](#setting-config-on-remote-servers) |
| 23 | +- [Contributing](#contributing) |
14 | 24 |
|
15 | | -# Installation |
| 25 | +<!-- /MarkdownTOC --> |
| 26 | + |
| 27 | + |
| 28 | +[12-factor]: http://12factor.net/ |
| 29 | + |
| 30 | +# Usages |
| 31 | + |
| 32 | +`.env` is a simple text file. With each environment variables listed per line, in the format of `KEY="Value"` |
16 | 33 |
|
| 34 | +<pre> |
| 35 | +SECRET_KEY="your_secret_key" |
| 36 | +DATABASE_PASSWORD="your_database_password" |
| 37 | +... |
| 38 | +</pre> |
| 39 | + |
| 40 | +Assuming you have created the `.env` file along-side your settings module. |
17 | 41 | ``` |
18 | | -pip install python-dotenv --upgrade |
| 42 | +. |
| 43 | +├── .env |
| 44 | +└── settings.py |
19 | 45 | ``` |
20 | 46 |
|
21 | | -# Usage |
| 47 | +Add the following code to your `settings.py` |
22 | 48 |
|
23 | | -## Loading variables from a `.env` file into your python project |
| 49 | +```python |
| 50 | +# settings.py |
| 51 | +from os.path import join, dirname |
| 52 | +from dotenv import load_dotenv |
24 | 53 |
|
25 | | -### Any Python Project |
| 54 | +dotenv_path = join(dirname(__file__), '.env') |
| 55 | +load_dotenv(dotenv_path) |
| 56 | +``` |
26 | 57 |
|
27 | | -Add the following line at the start of the file, from your program starts: |
| 58 | +Now, you can access the variables either from existing environment variable or loaded from `.env` file. `.env` file gets higher precedence, and it's adviced not to include it in version control. |
28 | 59 |
|
29 | 60 | ```python |
30 | | -import dotenv |
31 | | -dotenv.load_dotenv("/path/to/.env") |
| 61 | +# settings.py |
| 62 | + |
| 63 | +SECRET_KEY = os.environ.get("SECRET_KEY") |
| 64 | +DATABASE_PASSWORD = os.environ.get("DATABASE_PASSWORD") |
32 | 65 | ``` |
33 | 66 |
|
34 | 67 | ### Django |
35 | 68 |
|
36 | | -If you are using django you should add the above loader script at the top of `settings.py` and `manage.py`. |
37 | | - |
38 | | -NOTE: If you use [django-configurations], support for reading `.env` file is coming soon[1]! |
| 69 | +If you are using django you should add the above loader script at the top of `wsgi.py` and `manage.py`. |
39 | 70 |
|
40 | | -[1] https://github.com/jezdez/django-configurations/commit/01e3f5837f3d0fed215d |
41 | | - |
42 | | -[django-configurations]: https://github.com/jezdez/django-configurations |
43 | 71 |
|
| 72 | +# Installation |
44 | 73 |
|
45 | | -## Format of `.env` file |
| 74 | +``` |
| 75 | +pip install python-dotenv --upgrade |
| 76 | +``` |
46 | 77 |
|
47 | | -`.env` is a simple text file. With each environment variables listed per line, in the format of `KEY="Value"` |
48 | 78 |
|
49 | | -<pre> |
50 | | -SECRET_KEY="your_secret_key" |
51 | | -DATABASE_PASSWORD="your_database_password" |
52 | | -... |
53 | | -</pre> |
| 79 | +# Command-line interface |
54 | 80 |
|
55 | | -## Command-line interface |
| 81 | +A cli interface `dotenv` is also included, which helps you manipulate the `.env` file without manually opening it. The same cli installed on remote machine combined with fabric (discussed later) will enable you to update your settings on remote server, handy isn't it! |
56 | 82 |
|
57 | 83 | <pre> |
58 | 84 | $ dotenv |
@@ -155,6 +181,6 @@ That's it. example.com, or whoever your non-paas host is, is now 1 facor closer |
155 | 181 |
|
156 | 182 | # Contributing |
157 | 183 |
|
158 | | -Please open [an issue] or send us a pull request. |
| 184 | +All the contributions are welcome! Please open [an issue] or send us a pull request. |
159 | 185 |
|
160 | 186 | [an issue]: https://github.com/theskumar/python-dotenv/issues/new |
0 commit comments