Skip to content

manishy635/python-env

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python and Django Environment Variables

A simple module which will allow python apps and webapps to read .env files (forman style); storing the key-value in the os environment variables.

Installation:

Install via pip: pip install envvars or by download the package. Running python setup.py.

Usage:

Loading environment variables into os.environ:

Add the following line to your main python file.

import envvars

Filepath can be blank if .env is in current working directory

envvars.load() or envvars.load('/path/to/file')

Django

To load .env into django add the following lines of code into the manage.py and wsgi.py files.

Example .env file:

DJANGO_SETTINGS_MODULE = settings.dev
DJANGO_SECRETKEY = somerandomkey

Example manage.py file:

#!/usr/bin/env python
import os
import sys

import envvars

if __name__ == "__main__":
    # sets DJANGO_SETTINGS_MODULE='settings.dev'
    envars.load()

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Example settings.py file:

import envvars
SECRET_KEY = envvars.get('DJANGO_SECRETKEY')

Using environment variables:

You can use the set environment variables using either os.environ or envvars.get. envvars.get has the added advantage of converting to python type. Therefore, a key value set of KEY=True will be returned as a bool type not string as os.environ would normally do.

Envvars.get can be used within your application by:

# key=12345
return_val = envvars.get('key')
> 1234 # type int

About

Read .env file (key->value) setting values as environment variables

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%