1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
# envs
Easy access of environment variables from Python with support for booleans, strings, lists, tuples, integers, floats, and dicts.
## Use Case
If you need environment variables for your settings but need an easy way of using Python objects instead of just strings. For example, if you need a list of strings.
## Features
- CLI to convert settings
- CLI to list and check environment variables
- Use strings, lists, tuples, integers, floats or dicts. **IMPORTANT:** When setting the variables in your environmenet (ex. in .env file) wrap them in single or double quotes (ex. `"['some','list']"`)
[](https://travis-ci.org/capless/envs)
## Quick Start
### Install
#### Install without CLI Requirements
```commandline
pip install envs
```
#### Install with CLI Requirements
```commandline
pip install envs["cli"]
```
### Run Convert Settings
**IMPORTANT:** Don't name this file the same as the original module (you have added the imports back yet).
```commandline
envs convert_settings --settings-file your.settings.module
```
### Copy and Paste the Imports and Logic Code From Original File
Envs does not copy and paste your imports from your original code, so you have to do this manually.
### Run List Envs
This tells you what envs have default v
```commandline
envs list_envs --settings-file your.settings.module
```
## General API
```python
from envs import env
env('SOMEVAR','default value for that var',var_type='string',allow_none=True)
```
### Strings
**Environment Variable Example:** SECRET_KEY='adfadfadfafasf'
```python
>>>from envs import env
>>>env('SECRET_KEY','default secret key here')
'adfadfadfafasf'
```
### Lists
**Environment Variable Example:** SERVER_NAMES="['coastal','inland','western']"
```python
>>>from envs import env
>>>env('SERVER_NAMES',var_type='list')
['coastal','inland','western']
```
### Tuples
**Environment Variable Example:** SERVER_NAMES="('coastal','inland','western')"
```python
>>>from envs import env
>>>env('SERVER_NAMES',var_type='tuple')
('coastal','inland','western')
```
### Dicts
**Environment Variable Example:** DATABASE="{'USER':'name','PASSWORD':'password'}"
```python
>>>from envs import env
>>>env('DATABASE',var_type='dict')
{'USER':'name','PASSWORD':'password'}
```
### Integers
**Environment Variable Example:** NO_SERVERS=12
```python
>>>from envs import env
>>>env('NO_SERVERS',var_type='integer')
12
```
### Floats
**Environment Variable Example:** INDEX_WEIGHT=0.9
```python
>>>from envs import env
>>>env('INDEX_WEIGHT',var_type='float')
0.9
```
### Booleans
**Environment Variable Example:** USE_PROFILE=false
```python
>>>from envs import env
>>>env('USE_PROFILE',var_type='boolean')
False
```
### Decimals
**Environment Variable Example:** HALF_SEVEN=3.5
```python
>>> from envs import env
>>> env('HALF_SEVEN', var_type='decimal')
Decimal('3.5')
```
## Command Line Utils
**IMPORTANT:** All of the command arguments will fallback to becoming prompts if not set when calling the commands.
### Convert Module (convert_module)
Converts an existing settings file so it uses envs. **IMPORTANT:** This command does not copy your **import** stataements to the new module.
#### Arguments
- **settings-file:** - Dot notated import string for settings file
```commandline
envs convert_module --settings-file your.settings
```
### List Envs (list_envs)
Shows a list of env instances set in a settings file.
- **settings-file:** - Dot notated import string for settings file
- **keep-result:** - Keep the .env_results file generated by this command (**default:** False)
```commandline
envs list_envs --settings-file your.settings --keep-result False
```
### Check Envs (check_envs)
Make sure that the defined envs with no default value have a value set in the environment. This command will raise an **EnvsValueException** if there is environment variable that should be set that is not. This command is meant for use with a CI/CD tool as a way to halt the build if there isn't a value for an environment variable.
- **settings-file:** - Dot notated import string for settings file
```commandline
envs check_envs --settings-file your.settings
```
### Author
**Twitter:**:[@brianjinwright](https://twitter.com/brianjinwright)
**Github:** [bjinwright](https://github.com/bjinwright)
|