• Stars
    star
    566
  • Rank 76,033 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Loads environment variables from .env

django-dotenv

build-status-image pypi-version

foreman reads from .env. manage.py doesn't. Let's fix that.

Original implementation was written by @jacobian.

Tested on Python 3.5, 3.6, 3.7 and 3.8.

Installation

pip install django-dotenv

Usage

Your manage.py should look like:

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

import dotenv


if __name__ == "__main__":
    dotenv.read_dotenv()

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

You can also pass read_dotenv() an explicit path to the .env file, or to the directory where it lives. It's smart, it'll figure it out.

By default, variables that are already defined in the environment take precedence over those in your .env file. To change this, call read_dotenv(override=True).

Check out tests.py to see all the supported formats that your .env can have.

Using with WSGI

If you're running Django with WSGI and want to load a .env file, your wsgi.py would look like this:

import os

import dotenv
from django.core.wsgi import get_wsgi_application

dotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

application = get_wsgi_application()

That's it. Now go 12 factor the crap out of something.

Common problems

AttributeError: module 'dotenv' has no attribute 'read_dotenv'

There is another similar package, python-dotenv, which also contains a module called dotenv. If that package is installed, then you will see:

AttributeError: module 'dotenv' has no attribute 'read_dotenv'

To resolve this, uninstall python-dotenv.

read_dotenv is not reading from my environment file!

By default, variables that are already defined in the environment take precedence over those in your .env file. To change this, call read_dotenv(override=True).

More Repositories

1

pyjwt

JSON Web Token implementation in Python
Python
4,883
star
2

django-rest-framework-jwt

JSON Web Token Authentication support for Django REST Framework
Python
3,185
star
3

django-project-template

Project template layout for Django 3.0+
Python
691
star
4

juicebox

A virtual machine designed for programming workshops.
Shell
533
star
5

redisapp

The easiest way to get started with Redis on the Mac
Objective-C
351
star
6

django-jwt-auth

JSON Web Token Authentication support for Django
Python
164
star
7

black-playground

psf/black online demo
JavaScript
156
star
8

cookiecutter-django-rest-framework

A cookiecutter template for creating reusable Django REST Framework packages quickly.
Python
90
star
9

django-rest-framework-xml

XML support for Django REST Framework
Python
83
star
10

rabbitmqapp

The easiest way to get started with RabbitMQ on the Mac
Objective-C
66
star
11

django-rest-framework-oauth

OAuth support for Django REST Framework
Python
59
star
12

ember-fastboot-docker

Dockerized Node.js server for running Ember FastBoot apps
JavaScript
41
star
13

postmark-inbound-python

Python Wrapper for Postmark Inbound
Python
37
star
14

feedleap

Your RSS feeds as Clips in a Kippt List
CSS
36
star
15

django-rest-framework-yaml

YAML support for Django REST Framework
Python
31
star
16

ember-poe

Simple Markdown editor Ember Application using localStorage
CoffeeScript
29
star
17

tumblr-to-ghost

Tumblr exporter to Ghost importer
Python
26
star
18

telegram-gate

Las 889 páginas de Telegram entre Rosselló Nevares y sus allegados
JavaScript
26
star
19

yapf-online

google/yapf online demo
HTML
24
star
20

tracking-luma-outages

Record de datos de interrupciones de servicio reportados por LUMA Energy en Puerto Rico a través de el portal de Mi LUMA.
Python
24
star
21

django-rest-framework-jsonp

JSONP support for Django REST Framework
Python
20
star
22

mandrill-inbound-python

Python Wrapper for Mandrill Inbound Email
Python
20
star
23

notaso

Conoce a tus profesores antes de llegar al salón.
JavaScript
20
star
24

dotfiles

This is a collection of my dotfiles for Mac OS X.
Shell
18
star
25

statsd-graphite-vm

Vagrant VM for StatsD + Graphite
Ruby
16
star
26

ivona-go

Go client library for IVONA Speech Cloud API
Go
15
star
27

heroku-flower

Deploy Flower to Heroku
14
star
28

tracking-status-pr

This tool scrapes status.pr every hour and keeps tracks of changing metrics in order to help visualize and measure progress.
Python
14
star
29

codeigniter-s3-swfupload

Uploading directly to S3 using Codeigniter and swfUpload
13
star
30

HackerDesignerNews

Hacker News and Designer News Aggregator and Web API
JavaScript
13
star
31

designer-news-python

Python library for the Designer News API
Python
11
star
32

barcampinator

Barcamp user registration tool + presentation display
JavaScript
10
star
33

ember-clipboard

Ember.js addon component for clipboard.js
JavaScript
8
star
34

thenews-api

Scraper and Web API for thenews.im
JavaScript
7
star
35

WallIt

A fun geolocation app powered by ACS. Inspired by Anywall(which is powered by Parse)
JavaScript
6
star
36

alchemyapi-go

Go client library for AlchemyAPI
Go
6
star
37

tracking-gov-pr

Tracking gov.pr properties
Python
5
star
38

apex-chat

Las 72 páginas del chat de APEX entre Robert Rodriguez y sus allegados
JavaScript
3
star
39

datasette-mortalidad

Herramienta para explorar la Base de Datos de Mortalidad en Puerto Rico
Python
3
star
40

better-endi

Browser extensions to bypass transition ads in endi.com
JavaScript
2
star
41

npm-repo

Redirect to an npm package's repository
JavaScript
2
star
42

vagrant-example

Playing around with Vagrant and Chef
Ruby
1
star
43

bokamerki

CSS
1
star
44

oghowto

1
star
45

email-hangman

Hangman via email
Python
1
star
46

rttm

Go
1
star
47

py-backwards-online

py-backwards online demo
HTML
1
star
48

auth0-quiz

A Quiz generator for questions regarding security, authorization and authentication. You can use this to create any quiz you want.
JavaScript
1
star
49

glimmer-electron-demo

Run glimmer in electron with the help of ember-electron
JavaScript
1
star
50

remindeat

Never forget what you've already ate at a restaurant
Python
1
star
51

ember-hn-clone

A Hacker News / Designer News clone built with Ember.js
JavaScript
1
star