Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

manishy635/python-env

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

.env files for Python and Django

Build Status Downloads this month

A .env file is a text file holding values which will be added into the applications environment variables. The file uses the following format; a single key=value pair on each line.

Installation:

Install via pip: pip install python-env 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 dotenv

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

dotenv.load() or dotenv.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 dotenv

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Example settings.py file:

import dotenv
SECRET_KEY = dotenv.get('DJANGO_SECRETKEY', default='fall back value')

Using environment variables:

You can use the set environment variables using either os.environ or dotenv.get. dotenv.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.

dotenv.get can be used within your application by:

# key=12345
return_val = dotenv.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%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.