Easy access of environment variables from Python with support for booleans, strings, lists, tuples, integers, floats, and dicts.
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.
- 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']"
)
pip install envs
pip install envs["cli"]
IMPORTANT: Don't name this file the same as the original module (you have added the imports back yet).
envs convert_settings --settings-file your.settings.module
Envs does not copy and paste your imports from your original code, so you have to do this manually.
This tells you what envs have default v
envs list_envs --settings-file your.settings.module
from envs import env
env('SOMEVAR','default value for that var',var_type='string',allow_none=True)
Environment Variable Example: SECRET_KEY='adfadfadfafasf'
>>>from envs import env
>>>env('SECRET_KEY','default secret key here')
'adfadfadfafasf'
Environment Variable Example: SERVER_NAMES="['coastal','inland','western']"
>>>from envs import env
>>>env('SERVER_NAMES',var_type='list')
['coastal','inland','western']
Environment Variable Example: SERVER_NAMES="('coastal','inland','western')"
>>>from envs import env
>>>env('SERVER_NAMES',var_type='tuple')
('coastal','inland','western')
Environment Variable Example: DATABASE="{'USER':'name','PASSWORD':'password'}"
>>>from envs import env
>>>env('DATABASE',var_type='dict')
{'USER':'name','PASSWORD':'password'}
Environment Variable Example: NO_SERVERS=12
>>>from envs import env
>>>env('NO_SERVERS',var_type='integer')
12
Environment Variable Example: INDEX_WEIGHT=0.9
>>>from envs import env
>>>env('INDEX_WEIGHT',var_type='float')
0.9
Environment Variable Example: USE_PROFILE=false
>>>from envs import env
>>>env('USE_PROFILE',var_type='boolean')
False
Environment Variable Example: HALF_SEVEN=3.5
>>> from envs import env
>>> env('HALF_SEVEN', var_type='decimal')
Decimal('3.5')
IMPORTANT: All of the command arguments will fallback to becoming prompts if not set when calling the commands.
Converts an existing settings file so it uses envs. IMPORTANT: This command does not copy your import stataements to the new module.
- settings-file: - Dot notated import string for settings file
envs convert_module --settings-file your.settings
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)
envs list_envs --settings-file your.settings --keep-result False
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
envs check_envs --settings-file your.settings
Twitter::@brianjinwright Github: bjinwright