Skip to main content

modconfig – Simple hierarchic configuration manager for apps

Tests Status PYPI Version

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 to specify default configuration options for various environments and optionally override them by custom environment variables.

modconfig uses python modules for keep the configuration options. You are not locked by format (json, yaml, ini) restrictions and able to use any python statements/modules to tune your configuration as a pro. It keeps the flexability and make your configuration very declarative without any magic.

Requirements

  • python >= 3.6

Installation

modconfig should be installed using pip:

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 https://github.com/klen/modconfig/tree/develop/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

Fallbacks

If you provide a several modules, modconfig will be using the first available:

from modconfig import Config

cfg = Config('myapp.config.local', 'myapp.config.production', ANY_OPTION1="VALUE")

assert cfg.DATABASE
assert cfg.ANY_OPTION1

Enviroments

The module path may be set as 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 redifened with ENV variables. By default the modconfig tries to parse value as a JSON which allows us to set complex values (dict, list, etc). If value is not JSON it would be parsed as str.

Any ENV variables which names are not contained in source module would be ignored.

See https://github.com/klen/modconfig/tree/develop/tests.py for more examples.

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

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.2.1
File Size Uploaded
modconfig-1.2.1.tar.gz 5.4 kB Details

Built distribution (wheel)

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

Total release size: 11.0 kB

Release files / modconfig-1.2.1.tar.gz

Download URL modconfig-1.2.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
50cf0170718556ad3bc6f86eb8ef3464f6f24aedeb80dc8c624888d4952243c6
BLAKE2b-256 checksum
How to use checksums
b7e1e7affd3a0cbafa2f2884b86e9bd25290925b4a0d817099b830543961763d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / modconfig-1.2.1-py3-none-any.whl

Download URL modconfig-1.2.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb9f7a039211488e959ed23aed8abf6514bac124872fc348865f74ab9f6ca9c5
BLAKE2b-256 checksum
How to use checksums
5b939ab0eed49ea2231541720dc8f346660403fad622ee9aa14a15bbadf55bad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.16.0

1 release file

0.15.1

1 release file

0.15.0

1 release file

0.14.1

1 release file

0.14.0

1 release file

0.13.1

1 release file

0.13.0

1 release file

0.12.0

1 release file

0.11.0

1 release file

0.10.2

1 release file

0.10.1

1 release file

0.10.0

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.8.0

1 release file

0.7.1

1 release file

0.6.0

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.3

1 release file

0.2.6

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.0.0

1 release file

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