Skip to main content
https://secure.travis-ci.org/Polyconseil/getconf.png?branch=master Latest Version Supported Python versions Wheel status License

The getconf project provides simple configuration helpers for Python programs.

It provides a simple API to read from various configuration files and environment variables:

import getconf
config = getconf.ConfigGetter('myproj', ['/etc/myproj.conf'])
db_host = config.getstr('db.host', 'localhost')
db_port = config.getint('db.port', 5432)

Beyond this API, getconf aims at unifying configuration setup across development and production systems, respecting the standard procedures in each system:

  • Allow userspace configuration on development systems

  • Allow multiple different configurations for continuous integration systems

  • Use standard configuration space in /etc on traditional production servers

  • Handle environment-based configuration for cloud-based platforms

getconf is distributed under the two-clause BSD license, a copy of which is in the source.

getconf v1.10 onwards supports Python 3.5, 3.6, 3.7, 3.8 and 3.9. v1.9.x are the last versions to support Python 2.7 and 3.4. v1.8.x are the last versions to support Python 3.3. v1.5.x are the last versions to support Python 2.6.

Installation

Install the package from PyPI, using pip:

pip install getconf

Or from GitHub:

git clone git://github.com/Polyconseil/getconf

getconf has no external dependency beyond Python.

Introduction

All configuration values are accessed through the getconf.ConfigGetter object:

import getconf
config = getconf.ConfigGetter('myproj', ['/etc/myproj/settings.ini', './local_settings.ini'])

The above line declares:

  • Use the myproj namespace (explained later; this is mostly used for environment-based configuration, as a prefix for environment variables)

  • Look, in turn, at /etc/myproj/settings.ini (for production) and ./local_settings.ini (for development); the latter overriding the former.

Once the getconf.ConfigGetter has been configured, it can be used to retrieve settings:

debug = config.getbool('debug', False)
db_host = config.getstr('db.host', 'localhost')
db_port = config.getint('db.port', 5432)
allowed_hosts = config.getlist('django.allowed_hosts', ['*'])

All settings have a type (default is text), and accept a default value. They use namespaces (think ‘sections’) for easier reading.

With the above setup, getconf will try to provide db.host by inspecting the following options in order (it stops at the first defined value):

  • From the environment variable MYPROJ_DB_HOST, if defined

  • From the host key in the [db] section of ./local_settings.ini

  • From the host key in the [db] section of /etc/myproj/settings.ini

  • From the default provided value, 'localhost'

Features

Env-based configuration files

An extra configuration file/directory/glob can be provided through MYPROJ_CONFIG; it takes precedence over other files

Default options

An extra dictionary can be provided as ConfigGetter(defaults=some_dict); it is used after configuration files and environment variables.

It should be a dict mapping a section name to a dict of key => value:

>>> config = ConfigGetter('myproj', defaults={'db': {'host': 'localhost'}})
>>> config.getstr('db.host')
'localhost'
Typed getters

getconf can convert options into a few standard types:

config.getbool('db.enabled', False)
config.getint('db.port', 5432)
config.getlist('db.tables')  # Expects a comma-separated list
config.getfloat('db.auto_vacuum_scale_factor', 0.2)
config.gettimedelta('account_activation.validity', '2d')
config.getpath('django.static_root', pathlib.Path(BASE_DIR / 'static'))

getconf can also convert options to user-defined standard-type-based types:

class Environment(str, enum.Enum):
    DEV = 'dev'
    PROD = 'prod'
config.getenum('environment', Environment.PROD)

Concepts

getconf relies on a few key concepts:

namespace

Each ConfigGetter works within a specific namespace (its first argument).

Its goal is to avoid mistakes while reading the environment: with ConfigGetter(namespace='myproj'), only environment variables beginning with MYPROJ_ will be read.

It is, however, possible to disable namespacing by using ConfigGetter(namespace=getconf.NO_NAMESPACE).

Sections

The configuration options for a project often grow quite a lot; to restrict complexity, getconf splits values into sections, similar to Python’s configparser module.

Section are handled differently depending on the actual configuration source:

  • section.key is mapped to MYPROJ_SECTION_KEY for environment variables

  • section.key is mapped to [section] key = in configuration files

  • section.key is mapped to defaults['section']['key'] in the defaults dict.

Default section

Some settings are actually “globals” for a projet. This is handled by unset section names:

  • key is mapped to MYPROJ_KEY for environment variables

  • key is mapped to [DEFAULT] key = in configuration files

  • key is mapped to defaults['DEFAULT']['key'] in the defaults dict.

Release files for getconf 1.11.0

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

Source distribution (sdist)

Source distribution for getconf 1.11.0
File Size Uploaded
getconf-1.11.0.tar.gz 28.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for getconf 1.11.0
File Interpreter ABI Platform
getconf-1.11.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size:38.3 kB

Release files / getconf-1.11.0.tar.gz

Download URL getconf-1.11.0.tar.gz
Size 28.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f9f7eb0f258f6bd2e9e9030f9728fa7d6a0fc7d3061c01d3ad16b861d8951867
BLAKE2b-256 checksum
How to use checksums
3186e0510ced1bd385e731aa4b3834b3b2a4bf1c573ad75f1aaf9eb35de3134e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / getconf-1.11.0-py2.py3-none-any.whl

Download URL getconf-1.11.0-py2.py3-none-any.whl
Size 10.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
927e75f60429f48cb996b3ebd8e5eb396e18138cd9653f332d49098cd642674a
BLAKE2b-256 checksum
How to use checksums
6949274182ecf736f4dcfe9f38717f018f95ea63f4274ecc37e0ace7ccc1f6b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

1.11.1

2 release files

This release

1.11.0 This release

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.2

1 release file

1.4.1

2 release files

1.4.0

2 release files

1.3.0

1 release file

1.2.1

1 release file

1.1.0

1 release file

1.0.1

1 release file

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