A Python configuration management library that merges values from multiple sources (files, command line, environment variables) with schema validation and configurable priority ordering.
pip install the_conf
# or
poetry add the_confCreate a YAML file defining your configuration schema (myapp.meta.yml):
source_order: ['env', 'files', 'cmd'] # Priority order (first wins)
config_files: ['config.yml']
parameters:
- database_url:
type: str
required: true
help_txt: Database connection string
- debug:
type: bool
default: false
- max_connections:
type: int
default: 10
among: [5, 10, 20, 50] # Valid choices
- nested:
- timeout:
type: int
default: 30from the_conf import TheConf
# Load meta configuration and gather values from all sources
conf = TheConf('myapp.meta.yml')
# Access values
print(conf.database_url)
print(conf.debug)
print(conf.nested.timeout)
# Modify values (writes to main config file)
conf.max_connections = 20
conf.write()From environment variables:
export DATABASE_URL="postgresql://localhost/mydb"
export NESTED_TIMEOUT="60"From config file (config.yml):
database_url: postgresql://localhost/mydb
debug: true
nested:
timeout: 45From command line:
python myapp.py --database-url postgresql://localhost/mydb --debug --nested-timeout 60The source_order defines which source takes precedence when the same parameter is provided from multiple sources. The first source in the list wins - later sources do not overwrite values from earlier sources.
Default order: ["cmd", "files", "env"] (command line > files > environment)
Example with source_order: ["env", "files", "cmd"]:
- If
DEBUGis set in environment variables, the value from config files or command line will be ignored - This is useful when you want environment variables (e.g., in containers) to always take precedence
type:str,int,bool,list,dictdefault: Default value (cannot be combined withrequired)required: Must be provided from at least one sourceamong: List of valid choicesread_only: Prevents modification after initial loadno_cmd: Exclude this parameter from command line parsingno_env: Exclude this parameter from environment variable parsingcmd_line_opt: Override the auto-generated command line flaghelp_txt: Help text for documentation and CLI
parameters:
- allowed_ips:
type: list
allowed_ips: {type: str}From environment:
export ALLOWED_IPS_0="192.168.1.1"
export ALLOWED_IPS_1="192.168.1.2"From file:
allowed_ips:
- 192.168.1.1
- 192.168.1.2In Python:
conf.allowed_ips.append("192.168.1.3")
print(conf.allowed_ips[0]) # 192.168.1.1parameters:
- servers:
type: list
servers:
- host: {type: str}
- port: {type: int}From environment:
export SERVERS_0_HOST="localhost"
export SERVERS_0_PORT="8080"
export SERVERS_1_HOST="remote.host"
export SERVERS_1_PORT="8081"From file:
servers:
- host: localhost
port: 8080
- host: remote.host
port: 8081In Python:
print(conf.servers[0].host) # localhost
print(conf.servers[0].port) # 8080Note: List parameters are not available via command line arguments due to technical limitations.
the_conf supports encrypted configuration files using AES encryption:
# Save encrypted config
conf = TheConf('myapp.meta.yml')
conf.database_password = "secret"
conf.write('config.yml', passkey='my-encryption-key')
# Load encrypted config
conf = TheConf('myapp.meta.yml', passkey='my-encryption-key')Or via command line/environment:
python myapp.py --passkey my-encryption-key
# or
export THECONF_PASSKEY="my-encryption-key"Create hierarchical configuration structures:
parameters:
- database:
- host: {type: str, default: localhost}
- port: {type: int, default: 5432}
- credentials:
- username: {type: str}
- password: {type: str}Access with dot notation:
conf.database.host
conf.database.credentials.usernameUse the interactive mode to generate configuration files:
conf = TheConf('myapp.meta.yml', prompt_values=True)
# Prompts user for required values and generates config fileFrom this article, a good configuration library should:
- Provide a standardized API to define parameters via a data schema
- Generate command line and environment variable parsers from the schema
- Generate validators from the schema
- Separate program configuration from user parameters
- Support read-only settings and permissions
- Load settings from compatible sources (database, files, APIs, services)
- Support configuration hierarchies with cascading value retrieval
- Be simple enough for small scripts yet powerful for complex applications
- Auto-document settings
- Meta Configuration: The schema file (YAML/JSON) that defines parameter names, types, defaults, and validators
- User Configuration: The actual values loaded from files, command line, or environment variables
# Run tests
make test
# Run linters
make lint
# Build package
make buildGPLv3 - See LICENSE file for details