modconfig
Simple hierarchic configuration manager for apps.
Motivation
Applications (especially web services) often require certain configuration
options to depend on the environment an application runs in (development,
testing, production, etc.). For instance, a database address config option may
default to a local database server during development, a mock database server
during testing, and yet another database server during production. It may also
need to be customizable via an environment variable. modconfig approaches
scenarios like this, and allows you to specify default configuration options
for various environments and optionally override them by custom environment
variables.
modconfig uses Python modules to keep the configuration options. You are not
locked by format (json, yaml, ini) restrictions and are able to use any Python
statements/modules to tune your configuration as a pro. It keeps the
flexibility and makes your configuration very declarative without any magic.
Requirements
- Python >= 3.11
Installation
pip install modconfig
Usage
For example you have the structure in your app:
|- myapp/
| |- __init__.py
| |- config/
| | |- __init__.py
| | |- defaults.py
| | |- production.py
| | |- stage.py
| | |- tests.py
| | ...
See the example as a simple reference.
Initialize the config in your app and use it anywhere:
from modconfig import Config
cfg = Config(
# instead an import path it could be the module itself
"myapp.config.production",
# Override any options
ANY_OPTION1="VALUE",
ANY_OPTION2="VALUE",
)
assert cfg.DATABASE
assert cfg.ANY_OPTION1
Option names are case-insensitive: cfg.DATABASE, cfg.database and
cfg["database"] return the same value. The type of an option comes from the
first value it receives, or from a bare type hint in the config module
(LIMIT: int = 5); anything set later is converted to that type, and a value
that cannot be converted is logged and ignored.
Fallbacks
With several modules, modconfig uses the first one it can import:
from modconfig import Config
cfg = Config("myapp.config.local", "myapp.config.production", ANY_OPTION1="VALUE")
assert cfg.DATABASE
assert cfg.ANY_OPTION1
Environments
The module path may be set as an ENV variable:
import os
from modconfig import Config
# Let's define an env var
os.environ["MODCONFIG"] = "myapp.config.production"
cfg = Config("env:MODCONFIG", "myapp.config.local")
assert cfg.DATABASE
Custom environment variables
Any option may be overridden with an ENV variable. Values are parsed as JSON when possible, so dicts and lists work; anything else stays a string.
ENV variables for options that do not exist in the config are ignored.
To pick up overrides only for your app, give them a prefix:
cfg = Config("myapp.config.production", config_config={"env_prefix": "APP_"})
# APP_DATABASE overrides DATABASE, DEMOENV is ignored
The meta options of the config itself (env_prefix, update_from_env) are
passed through config_config.
See tests.py for more examples.
Limitations
Configuration modules are Python code, so importing one executes it. Keep them as trusted as the application itself.
Only the first importable module in the list is used. The rest are fallbacks for when an earlier module is missing, not layers that get merged.
ENV overrides apply only to options that already exist in the config; unknown variables are ignored rather than added.
Because an option keeps the type of its first value, replacing an int with a
str later does not work: the string is converted back to an int or dropped.
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/modconfig/issues.
Contributing
Development of the project happens at https://github.com/klen/modconfig.
License
Licensed under a MIT license.
Metadata
Release files for modconfig 1.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| modconfig-1.3.2.tar.gz | 6.6 kB | Without provenance | Details for modconfig-1.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| modconfig-1.3.2-py3-none-any.whl | Python 3 | none | any | Details for modconfig-1.3.2-py3-none-any.whl |
Total release size: 13.6 kB
Release files / modconfig-1.3.2.tar.gz
| Download URL | modconfig-1.3.2.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b4cb85d6fb5112f1153bd9c1ed36d687374f726d77affae8050653c54156904
|
|
BLAKE2b-256 checksum How to use checksums |
b17374ec578bdb81339f1d4b9e7dda3e83054f2902c87fdde1c09cc5d6e7ec0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / modconfig-1.3.2-py3-none-any.whl
| Download URL | modconfig-1.3.2-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ebecb9530f27064e96ae340b25de63de3057efe2d3af96c753e1e8eb5e72eeb1
|
|
BLAKE2b-256 checksum How to use checksums |
0ba3b71abe5516665885b77b4fd811627f9d17480b6862c1afb7721982127a34
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for modconfig 1.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.