Skip to main content

modconfig

Tests PyPI Python License

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.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modconfig 1.3.3
File Size Uploaded Provenance Actions
modconfig-1.3.3.tar.gz 6.6 kB Without provenance Details for modconfig-1.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for modconfig 1.3.3
File Interpreter ABI Platform Actions
modconfig-1.3.3-py3-none-any.whl Python 3 none any Details for modconfig-1.3.3-py3-none-any.whl

Total release size: 13.6 kB

Release files / modconfig-1.3.3.tar.gz

Download URL modconfig-1.3.3.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
40b67c82ed835e23deea190ed29f1075090cc66e41bb7b1f90b66800fa7dae60
BLAKE2b-256 checksum
How to use checksums
d3829c8cd93adf1f89aadb9ecf0c35e5f86c257410b5ef76c6b8f87793c38e4c
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.3-py3-none-any.whl

Download URL modconfig-1.3.3-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d0de26de76544ea7f9fc44a77d20e065b2e708db694832ce5405e6658b784ad
BLAKE2b-256 checksum
How to use checksums
32fc48acd86e056c50bc8b3dbe47dd1936207302d22e86117d532f9b0ad2ce3a
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.3

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.

Release history Release notifications | RSS feed

This release

1.3.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page