environs: simplified environment variable parsing
environs is a Python library for parsing environment variables.
It allows you to store configuration separate from your code, as per
The Twelve-Factor App methodology.
Contents
Features
- Type-casting
- Read
.env
files into os.environ
(useful for local development) - Validation
- Define custom parser behavior
- Framework-agnostic, but integrates well with Flask and Django
Install
pip install environs
Basic usage
With some environment variables set...
export GITHUB_USER=sloria
export MAX_CONNECTIONS=100
export SHIP_DATE='1984-06-25'
export TTL=42
export ENABLE_LOGIN=true
export GITHUB_REPOS=webargs,konch,ped
export GITHUB_REPO_PRIORITY="webargs=2,konch=3"
export COORDINATES=23.3,50.0
export LOG_LEVEL=DEBUG
Parse them with environs...
from environs import Env
env = Env()
env.read_env()
gh_user = env("GITHUB_USER")
secret = env("SECRET")
max_connections = env.int("MAX_CONNECTIONS")
ship_date = env.date("SHIP_DATE")
ttl = env.timedelta("TTL")
log_level = env.log_level("LOG_LEVEL")
enable_login = env.bool("ENABLE_LOGIN", False)
enable_feature_x = env.bool("ENABLE_FEATURE_X", False)
gh_repos = env.list("GITHUB_REPOS")
coords = env.list("COORDINATES", subcast=float)
gh_repos_priorities = env.dict(
"GITHUB_REPO_PRIORITY", subcast_values=int
)
Supported types
The following are all type-casting methods of Env
:
env.str
env.bool
env.int
env.float
env.decimal
env.list
(accepts optional subcast
and delimiter
keyword arguments)env.dict
(accepts optional subcast_keys
, subcast_values
and delimiter
keyword arguments)env.json
env.datetime
env.date
env.time
env.timedelta
(assumes value is an integer in seconds, or an ordered duration string like 7h7s
or 7w 7d 7h 7m 7s 7ms 7us
)env.url
env.uuid
env.log_level
env.path
(casts to a pathlib.Path
)env.enum
(casts to any given enum type specified in type
keyword argument, accepts optional ignore_case
keyword argument)
Reading .env
files
DEBUG=true
PORT=4567
Call Env.read_env
before parsing variables.
from environs import Env
env = Env()
env.read_env()
env.bool("DEBUG")
env.int("PORT")
Reading a specific file
By default, Env.read_env
will look for a .env
file in current
directory and (if no .env exists in the CWD) recurse
upwards until a .env
file is found.
You can also read a specific file:
from environs import Env
with open(".env.test", "w") as fobj:
fobj.write("A=foo\n")
fobj.write("B=123\n")
env = Env()
env.read_env(".env.test", recurse=False)
assert env("A") == "foo"
assert env.int("B") == 123
Handling prefixes
with env.prefixed("MYAPP_"):
host = env("HOST", "localhost")
port = env.int("PORT", 5000)
with env.prefixed("MYAPP_"):
with env.prefixed("DB_"):
db_host = env("HOST", "lolcathost")
db_port = env.int("PORT", 10101)
Variable expansion
from environs import Env
env = Env(expand_vars=True)
connection_url = env("CONNECTION_URL")
year = env.int("YEAR")
Validation
from environs import Env
from marshmallow.validate import OneOf, Length, Email
env = Env()
env.int("TTL", validate=lambda n: n > 0)
env.str(
"NODE_ENV",
validate=OneOf(
["production", "development"], error="NODE_ENV must be one of: {choices}"
),
)
env.str("EMAIL", validate=[Length(min=4), Email()])
Deferred validation
By default, a validation error is raised immediately upon calling a parser method for an invalid environment variable.
To defer validation and raise an exception with the combined error messages for all invalid variables, pass eager=False
to Env
.
Call env.seal()
after all variables have been parsed.
from environs import Env
from marshmallow.validate import OneOf, Email, Length, Range
env = Env(eager=False)
TTL = env.int("TTL", validate=Range(min=0, max=100))
NODE_ENV = env.str(
"NODE_ENV",
validate=OneOf(
["production", "development"], error="NODE_ENV must be one of: {choices}"
),
)
EMAIL = env.str("EMAIL", validate=[Length(min=4), Email()])
env.seal()
env.seal()
validates all parsed variables and prevents further parsing (calling a parser method will raise an error).
URL schemes
env.url()
supports non-standard URL schemes via the schemes
argument.
REDIS_URL = env.url(
"REDIS_URL", "redis://redis:6379", schemes=["redis"], require_tld=False
)
Serialization
env.dump()
Defining custom parser behavior
from furl import furl
@env.parser_for("furl")
def furl_parser(value):
return furl(value)
domain = env.furl("DOMAIN")
@env.parser_for("choice")
def choice_parser(value, choices):
if value not in choices:
raise environs.EnvError("Invalid!")
return value
color = env.choice("COLOR", choices=["black"])
Usage with Flask
from environs import Env
env = Env()
env.read_env()
DEBUG = env.bool("FLASK_DEBUG", default=False)
SECRET_KEY = env.str("SECRET_KEY")
Load the configuration after you initialize your app.
from flask import Flask
app = Flask(__name__)
app.config.from_object("myapp.settings")
For local development, use a .env
file to override the default
configuration.
DEBUG=true
SECRET_KEY="not so secret"
Note: Because environs depends on python-dotenv,
the flask
CLI will automatically read .env and .flaskenv files.
Usage with Django
environs includes a number of helpers for parsing connection URLs. To
install environs with django support:
pip install environs[django]
Use env.dj_db_url
, env.dj_cache_url
and env.dj_email_url
to parse the DATABASE_URL
, CACHE_URL
and EMAIL_URL
environment variables, respectively.
For more details on URL patterns, see the following projects that environs is using for converting URLs.
Basic example:
from environs import Env
env = Env()
env.read_env()
DEBUG = env.bool("DEBUG", default=False)
SECRET_KEY = env.str("SECRET_KEY")
DATABASES = {"default": env.dj_db_url("DATABASE_URL")}
email = env.dj_email_url("EMAIL_URL", default="smtp://")
EMAIL_HOST = email["EMAIL_HOST"]
EMAIL_PORT = email["EMAIL_PORT"]
EMAIL_HOST_PASSWORD = email["EMAIL_HOST_PASSWORD"]
EMAIL_HOST_USER = email["EMAIL_HOST_USER"]
EMAIL_USE_TLS = email["EMAIL_USE_TLS"]
CACHES = {"default": env.dj_cache_url("CACHE_URL")}
For local development, use a .env
file to override the default
configuration.
DEBUG=true
SECRET_KEY="not so secret"
For a more complete example, see
django_example.py
in the examples/
directory.
Why...?
Why envvars?
See The 12-factor App section on
configuration.
Why not os.environ
?
While os.environ
is enough for simple use cases, a typical application
will need a way to manipulate and validate raw environment variables.
environs abstracts common tasks for handling environment variables.
environs will help you
- cast envvars to the correct type
- specify required envvars
- define default values
- validate envvars
- parse list and dict values
- parse dates, datetimes, and timedeltas
- parse expanded variables
- serialize your configuration to JSON, YAML, etc.
Why another library?
There are many great Python libraries for parsing environment variables.
In fact, most of the credit for environs' public API goes to the
authors of envparse and
django-environ.
environs aims to meet three additional goals:
- Make it easy to extend parsing behavior and develop plugins.
- Leverage the deserialization and validation functionality provided
by a separate library (marshmallow).
- Clean up redundant API.
See this GitHub
issue
which details specific differences with envparse.
License
MIT licensed. See the
LICENSE file
for more details.