Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active November 17, 2018 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remarkablemark/4c18bdd5321d900e25a5d8fb84c2968f to your computer and use it in GitHub Desktop.
Save remarkablemark/4c18bdd5321d900e25a5d8fb84c2968f to your computer and use it in GitHub Desktop.
Load .env with python-dotenv: https://github.com/theskumar/python-dotenv

Load .env with python

Install python-dotenv:

$ pip install python-dotenv

Given files:

.
├── .env
└── settings.py

.env:

# .env
FOO=BAR
ENV_VAR="hello world\n"

settings.py:

# settings.py

import os
from dotenv import load_dotenv

load_dotenv('.env')
print(os.getenv('FOO)) # 'BAR'

Run script:

$ python settings.py
BAR

Alternate ways of loading .env:

import os
form dotenv import load_dotenv, find_dotenv

# approach 1
load_dotenv(os.join(os.dirname(__file__), '.env')

# approach 2
load_dotenv(find_dotenv(usecwd=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment