Registry / devops / environs

environs

JSON →
library15.1.0pypypi✓ verified 25d ago

environs is a Python library (current version 15.0.1) designed for simplified parsing of environment variables, aligning with the Twelve-Factor App methodology for separating configuration from code. It provides robust type-casting, validation, and flexible parsing of various data types including lists, dictionaries, dates, and URLs. It also integrates seamlessly with `.env` files. environs is actively maintained and has a steady release cadence.

pip install environs
INSTALL
IMPORT
SIG · ENVIRONS
E
environs
devopspythonv15.1.0
Install
1.8s avg
Import
598ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v15.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.598s · 19MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.598s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Env
from environs import Env
from environs import env
While `from environs import env` works by importing a pre-instantiated `Env` object, directly importing `Env` and creating an instance (e.g., `env = Env()`) offers more control and clarity, especially when dealing with different environments or `eager=False`.

This quickstart demonstrates how to initialize `environs`, read various environment variables with type-casting, provide default values, and parse URLs. It shows how `environs` helps manage different data types from string-based environment variables. For local development with `.env` files, ensure `python-dotenv` is installed and `env.read_env()` is called.

import os from environs import Env from urllib.parse import urlparse import logging # For demonstration, set some environment variables os.environ['GITHUB_USER'] = 'test_user' os.environ['MAX_CONNECTIONS'] = '10' os.environ['SHIP_DATE'] = '1984-06-25' os.environ['ENABLE_LOGIN'] = 'true' os.environ['API_KEY'] = 'some_secret_key' os.environ['LOG_LEVEL'] = 'INFO' os.environ['MY_API_URL'] = 'https://api.example.com/v1' # Initialize Env object env = Env() # For local development, uncomment and ensure python-dotenv is installed: # with open('.env', 'w') as f: # f.write('ANOTHER_VAR=hello\n') # env.read_env() # Reads from .env file if it exists # Required variables gh_user = env('GITHUB_USER') print(f"GitHub User: {gh_user}") # Casting to specific types max_connections = env.int('MAX_CONNECTIONS', default=5) # default if not set print(f"Max Connections: {max_connections} (type: {type(max_connections)})") ship_date = env.date('SHIP_DATE') print(f"Ship Date: {ship_date} (type: {type(ship_date)})") enable_login = env.bool('ENABLE_LOGIN', default=False) print(f"Enable Login: {enable_login} (type: {type(enable_login)})") api_key = env('API_KEY') # No type cast, defaults to str print(f"API Key: {api_key}") log_level = env.log_level('LOG_LEVEL', default=logging.DEBUG) print(f"Log Level: {log_level} (type: {type(log_level)})") # URL parsing my_api_url = env.url('MY_API_URL') print(f"API URL: {my_api_url.scheme}://{my_api_url.netloc} (type: {type(my_api_url)})") # Example of a missing variable with default feature_flag = env.bool('FEATURE_X', default=False) print(f"Feature X enabled: {feature_flag}") # Example of a required variable not set (will raise EnvValidationError) # try: # missing_var = env('MISSING_REQUIRED_VAR') # except Exception as e: # print(f"Error: {e}")
Debug
Known issues
breakingThe `env.get()` method was removed in `environs` version 1.0.0. Attempting to use it will result in an `AttributeError`.
fix
Use the `Env` instance directly as a callable, e.g., `env('VAR_NAME')` for required variables or `env('VAR_NAME', default='fallback')` for optional variables with a default.
affects: >=1.0.0
gotchaReading `.env` files is not automatic. You must explicitly call `env.read_env()` early in your application's lifecycle to load variables from `.env` files into the `Env` instance. This functionality also requires `python-dotenv` to be installed.
fix
Ensure `pip install python-dotenv` is run and place `env.read_env()` at the beginning of your configuration loading logic.
affects: all
gotcha`environs` operates on its own internal state and does not directly mutate `os.environ` when `env.read_env()` is called or variables are accessed. If you later try to access `os.environ` directly, it will not reflect the values loaded or overridden by `environs` from `.env` files.
fix
Always use the `env` object provided by `environs` (e.g., `env('VAR_NAME')`, `env.int('NUMBER')`) throughout your application for configuration values to ensure consistency and proper type-casting. Avoid directly accessing `os.environ` for `environs`-managed values after initialization.
affects: all
gotchaThe `env.url()` method returns a `urllib.parse.ParseResult` object, not a plain string. If you require the URL as a string, you should explicitly use `env.str(..., validate=validate.URL())`.
fix
If a string URL is needed, use `from environs import Env, validate; url_string = env.str('MY_URL', validate=validate.URL())`. When providing a `default` value to `env.url()`, it must also be a `urllib.parse.ParseResult` object.
affects: all
gotchaWhen using validators with `environs` (especially when `Env` is initialized with `eager=False`), you must explicitly call `env.seal()` after all environment variables have been parsed to trigger deferred validation. Failing to do so can lead to validation errors not being raised until much later or incorrect behavior.
fix
Always call `env.seal()` after parsing all your required and optional environment variables if you are using validation, particularly with deferred (non-eager) validation.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'environ'
This error often occurs when you're trying to use `environs` but have inadvertently installed a different package named `environ` (or vice-versa), or if `environs` is not installed in your active Python environment. This can also happen if `django-environ` is intended but `environs` is installed instead, or if the virtual environment is not correctly activated.
fix
Ensure the correct package is installed in your active virtual environment. If you intend to use `environs`, run `pip install environs`. If you are working with Django and specifically intend `django-environ`, run `pip install django-environ`. Verify your active Python interpreter if using multiple environments.
AttributeError: module 'environ' has no attribute 'Env'
This specific `AttributeError` typically means you've imported a module named `environ` (a different, unrelated package) but are attempting to access the `Env` class which belongs to the `environs` library (or `django-environ`).
fix
Uninstall the incorrect `environ` package (`pip uninstall environ`) and then install the correct `environs` library (`pip install environs`). Make sure your import statement is `from environs import Env` or `import environs` followed by `env = environs.Env()`.
KeyError: 'YOUR_ENV_VARIABLE_NAME'
This `KeyError` indicates that `environs` cannot find an environment variable with the specified name. This can happen if the variable is not set in the operating system's environment, if the `.env` file containing it hasn't been loaded, or if there's a typo in the variable name.
fix
Ensure the environment variable is actually set where your application runs, or provide a default value when calling `env()` (e.g., `MY_VAR = env('MY_VAR', 'default_value')`). Confirm that `env.read_env()` is called before accessing variables and that your `.env` file is correctly located (usually in the project root) or its path is explicitly provided to `read_env()`.
Environment variables are None / .env file not loading
Developers often find that variables expected from their `.env` file are not being loaded into `os.environ` or `environs` returns `None` for them. Common reasons include the `.env` file being in the wrong directory, `env.read_env()` not being called, or existing system environment variables overriding those in the `.env` file (as `environs` by default does not override existing variables).
fix
Call `env.read_env()` early in your application's startup. Ensure the `.env` file is located in the current working directory of your script or provide its explicit path to `env.read_env(path='/path/to/.env')`. If you want `.env` values to override system variables, pass `override=True` to `read_env()` (e.g., `env.read_env(override=True)`).
Upgrade
Version history
15.1.0latest on PyPI · released Aug 1, 2026
Audit
Dependencies
python-dotenvrequiredRequired for reading .env files.
marshmallowrequiredUsed under the hood for validation, deserialization, and serialization.
dj-database-urloptionalOptional dependency for Django database URL parsing.
dj-email-urloptionalOptional dependency for Django email URL parsing.
django-cache-urloptionalOptional dependency for Django cache URL parsing.
Agent activity
17 hits · last 30 days
node
16
Resources
environs — pip install environs · libregistry