Environment
Load and manage environment configuration for the application.
Overview
The Environment class provides a unified interface for accessing configuration values from multiple sources. It looks in 4 possible places (in order of priority):
- Values set programmatically via the
set()method - Values from
os.environ - Values from a
.envfile - Secret values referenced via
secret://path/to/secret
The .env file should contain lines like NAME=value. Empty lines and lines starting with # are ignored. Values are automatically parsed into appropriate Python types (bool, int, float, or string).
Example usage:
import clearskies
# The environment is typically accessed via dependency injection
def my_function(environment: clearskies.Environment):
# Get a value (raises KeyError if not found)
database_url = environment.get("DATABASE_URL")
# Get a value silently (returns None if not found)
optional_value = environment.get("OPTIONAL_KEY", silent=True)
# Set a value programmatically (highest priority)
environment.set("MY_OVERRIDE", "custom_value")
# Or access it directly from the DI container
di = clearskies.di.Di()
environment = di.build("environment")
Example .env file:
# Database configuration
DATABASE_URL=postgresql://localhost/mydb
DEBUG=true
MAX_CONNECTIONS=10
TIMEOUT=30.5
# Secret reference (will be fetched from secret engine)
API_KEY=secret:///my/api/key
env_file_path
Required
The path to the .env file.
Defaults to .env in the current working directory. This can be overridden when constructing the Environment instance.