Skip to main content

pyconfig - Python-based singleton configuration

Build Status

This module provides python based configuration that is stored in a singleton object to ensure consistency across your project.

Table of Contents

Command Line

Pyconfig has a command line utility that lets you inspect your project to find all the configuration keys defined.

$ pyconfig -h
usage: pyconfig [-h] [-f F | -m M] [-v] [-l] [-a | -k] [-n] [-s] [-c]

Helper for working with pyconfigs

optional arguments:
  -h, --help          show this help message and exit
  -f F, --filename F  parse an individual file or directory
  -m M, --module M    parse a package or module, recursively looking inside it
  -v, --view-call     show the actual pyconfig call made (default: show namespace)
  -l, --load-configs  query the currently set value for each key found
  -a, --all           show keys which don't have defaults set
  -k, --only-keys     show a list of discovered keys without values
  -n, --natural-sort  sort by filename and line (default: alphabetical by key)
  -s, --source        show source annotations (implies --natural-sort)
  -c, --color         toggle output colors (default: True)

Examples

$ pyconfig --file .
humbledb.allow_explicit_request = True
humbledb.auto_start_request = True
humbledb.connection_pool = 300
humbledb.tz_aware = True
humbledb.use_greenlets = False
humbledb.write_concern = 1

$ pyconfig --view-call --file .
pyconfig.get('humbledb.allow_explicit_request', True)
pyconfig.setting('humbledb.auto_start_request', True)
pyconfig.setting('humbledb.connection_pool', 300)
pyconfig.setting('humbledb.tz_aware', True)
pyconfig.setting('humbledb.use_greenlets', False)
pyconfig.setting('humbledb.write_concern', 1)

$ pyconfig --source --file .
# ./humbledb/mongo.py, line 98
humbledb.allow_explicit_request = True
# ./humbledb/mongo.py, line 178
humbledb.connection_pool = 300
# ./humbledb/mongo.py, line 181
humbledb.auto_start_request = True
# ./humbledb/mongo.py, line 185
humbledb.use_greenlets = False
# ./humbledb/mongo.py, line 188
humbledb.tz_aware = True
# ./humbledb/mongo.py, line 191
humbledb.write_concern = 1

Etcd

Version added: 3.0.0

Pyconfig has read-only support for configurations stored in etcd, using the etcd v3 API via the etcd3 library. The preferred method for configuring Pyconfig to work with etcd is via ENV variables, since they must be set as early as possible. It is also possible to use the Python API to make Pyconfig work with etcd.

The etcd3 library is optional and not installed with Pyconfig. Install it alongside a compatible protobuf, since etcd3 0.12 does not work with protobuf 4 or later:

pip install "etcd3>=0.12.0,<0.13.0" "protobuf>=3.20.3,<3.21"

If etcd3 is installed but fails to import (for example, because of an incompatible protobuf), Pyconfig logs a warning and runs without etcd support.

Pyconfig uses a directory namespace to store its dot notation configuration key names. By default, that namespace is /config/. Only the keys directly under the namespace are loaded; keys in nested namespaces (like /config/app/dev/my.name when the namespace is /config/app/) are ignored.

At a minimum, PYCONFIG_ETCD_HOSTS must be set to get Pyconfig to try to read a configuration from etcd using the default settings.

You can set a value with etcdctl like:

# The etcdctl command is provided by etcd and not part of pyconfig
etcdctl put /pyconfig/example/my.setting "from etcd"

And configure Pyconfig to connect and use that setting:

$ export PYCONFIG_ETCD_PREFIX="/pyconfig/example/"
$ export PYCONFIG_ETCD_HOSTS="127.0.0.1:2379"
$ python
>>> import pyconfig
>>> pyconfig.get('my.setting')
'from etcd'

Because of Pyconfig's singleton nature, only one configuration can be accessed at a time in this way.

Environment variables:

  • PYCONFIG_ETCD_PREFIX - The namespace to prefix settings with (default: '/config/')
  • PYCONFIG_ETCD_HOSTS - A comma separated list of hosts, like 10.0.0.1:2379,10.0.0.2:2379. Note that the etcd3 client only connects to a single server, so only the first host is used.
  • PYCONFIG_ETCD_CACERT - CA cert file to use for SSL. The etcd3 client only uses TLS when this is set.
  • PYCONFIG_ETCD_CERT - Client cert file to use for SSL client authentication. Requires PYCONFIG_ETCD_KEY and PYCONFIG_ETCD_CACERT; without a CA cert, Pyconfig logs a warning and connects without TLS.
  • PYCONFIG_ETCD_KEY - Client private key file to use for SSL client auth
  • PYCONFIG_ETCD_WATCH - If this is set to a truthy value (a non-empty string), then pyconfig will keep the local configuration synchronized with etcd (Version added: 3.1.0)
  • PYCONFIG_ETCD_PROTOCOL - This should be a string of the form https or http. Note that the etcd3 client uses TLS only when a CA cert is provided, and cannot force HTTPS without one; if this is set to https without PYCONFIG_ETCD_CACERT, Pyconfig will log a warning and connect without TLS. (Version added: 3.2.0)
  • PYCONFIG_ETCD_AUTH - Set this to authenticate with a username and password. This should be a string of the format username:password. Without PYCONFIG_ETCD_CACERT the auth token is sent unencrypted. Expired auth tokens are refreshed automatically. (Version added: 3.2.0)

Inheritance:

If you want to create a configuration that inherits from an existing configuration, Pyconfig will look for a special key, which by default is set to config.inherit. If this exists and is set to an etcd namespace, that configuration will be used as the base for the current config.

A typical use case would be a Test environment configuration which is derived from a Development config. Below is a barebones example of how that might be set up using etcdctl and Pyconfig.

$ # Create the development settings
$ etcdctl put /config/app/dev/my.name example
$ etcdctl put /config/app/dev/my.hostname localhost
$ etcdctl put /config/app/dev/my.api.key abcdef0123456789
$ # Create the test settings
$ etcdctl put /config/app/test/my.hostname test.example.com
$ # Tell it to inherit from the development settings
$ etcdctl put /config/app/test/config.inherit /config/app/dev/
$ # Configure Pyconfig to use the test configuration
$ export PYCONFIG_ETCD_PREFIX="/config/app/test/"
$ export PYCONFIG_ETCD_HOSTS="127.0.0.1:2379"
$ python
>>> import pyconfig
>>> pyconfig.get('my.hostname')
'test.example.com'
>>> pyconfig.get('my.name')
'example'

Code Examples

The most basic usage allows you to get, retrieve and modify values. Pyconfig's singleton provides convenient accessor methods for these actions:

Version changed: 3.0.0

As of version 3.0.0, keys are not case sensitive by default.

>>> import pyconfig
>>> pyconfig.get('my.setting', 'default')
'default'
>>> pyconfig.set('my.setting', 'new')
>>> pyconfig.get('my.setting', 'default')
'new'
>>> pyconfig.reload(clear=True)
>>> pyconfig.get('my.setting', 'default')
'default'

You can also opt-out of default values:

>>> import pyconfig
>>> pyconfig.get('my.setting', allow_default=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pyconfig/__init__.py", line 275, in get
    return Config().get(name, default, allow_default=allow_default)
  File "pyconfig/__init__.py", line 234, in get
    return self.settings[name]
 LookupError: No setting "my.setting"

Pyconfig also provides shortcuts for giving classes property descriptors which map to the current setting stored in the singleton:

>>> import pyconfig
>>> class MyClass(object):
...     my_setting = pyconfig.setting('my.setting', 'default')
...
>>> MyClass.my_setting
'default'
>>> MyClass().my_setting
'default'
>>> pyconfig.set('my.setting', "Hello World!")
>>> MyClass.my_setting
'Hello World!'
>>> MyClass().my_setting
'Hello World!'
>>> pyconfig.reload(clear=True)
>>> MyClass.my_setting
'default'

The Setting class also supports preventing default values. When set this way, all reads on the attribute will prevent the use of defaults:

>>> import pyconfig
>>> class MyClass(object):
...     my_setting = pyconfig.setting('my.setting', allow_default=False)
...
>>> MyClass.my_setting
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pyconfig/__init__.py", line 84, in __get__
    allow_default=self.allow_default)
  File "pyconfig/__init__.py", line 232, in get
    raise LookupError('No setting "{}"'.format(name))
LookupError: No setting "my.setting"
>>> pyconfig.set('my.setting', 'new_value')
>>> MyClass.my_setting
'value'

Pyconfig allows you to override settings via a python configuration file, that defines its configuration keys as a module namespace. By default, Pyconfig will look on your PYTHONPATH for a module named localconfig, and if it exists, it will use this module namespace to update all configuration settings:

# __file__ = "$PYTHONPATH/localconfig.py"
from pyconfig import Namespace

# Namespace objects allow you to use attribute assignment to create setting
# key names
my = Namespace()
my.setting = 'from_localconfig'
# Namespace objects implicitly return new nested Namespaces when accessing
# attributes that don't exist
my.nested.setting = 'also_from_localconfig'

With a localconfig on the PYTHONPATH, it will be loaded before any settings are read:

>>> import pyconfig
>>> pyconfig.get('my.setting')
'from_localconfig'
>>> pyconfig.get('my.nested.setting')
'also_from_localconfig'

Pyconfig also allows you to create distutils plugins that are automatically loaded. An example setup.py:

# __file__ = setup.py
from setuptools import setup

setup(
        name='pytest',
        version='0.1.0-dev',
        py_modules=['myconfig', 'anyconfig'],
        entry_points={
            # The "my" in "my =" indicates a base namespace to use for
            # the contained configuration. If you do not wish a base
            # namespace, use "any"
            'pyconfig':[
                  'my = myconfig',
                  'any = anyconfig',
                  ],
            },
        )

An example distutils plugin configuration file:

# __file__ = myconfig.py
from pyconfig import Namespace

def some_callable():
    print "This callable was called."
    print "You can execute any arbitrary code."

setting = 'from_plugin'
nested = Namespace()
nested.setting = 'also_from_plugin'

Another example configuration file, without a base namespace:

# __file__ = anyconfig.py
from pyconfig import Namespace
other = Namespace()
other.setting = 'anyconfig_value'

Showing the plugin-specified settings:

>>> import pyconfig
>>> pyconfig.get('my.setting', 'default')
This callable was called.
You can execute any arbitrary code.
'from_plugin'
>>> pyconfig.get('my.nested.setting', 'default')
'also_from_plugin'
>>> pyconfig.get('other.setting', 'default')
'anyconfig_value'

More fancy stuff:

>>> # Reloading changes re-calls functions...
>>> pyconfig.reload()
This callable was called.
You can execute any arbitrary code.
>>> # This can be used to inject arbitrary code by changing a
>>> # localconfig.py or plugin and reloading a config... especially
>>> # when pyconfig.reload() is attached to a signal
>>> import signal
>>> signal.signal(signal.SIGUSR1, pyconfig.reload)

Pyconfig provides a @reload_hook decorator that allows you to register functions or methods to be called when the configuration is reloaded:

>>> import pyconfig
>>> @pyconfig.reload_hook
... def reload():
...     print "Do something here."
...
>>> pyconfig.reload()
Do something here.

Warning: It should not be used to register large numbers of functions (e.g. registering a bound method in a class's __init__ method), since there is no way to un-register a hook and it will cause a memory leak, since a bound method maintains a strong reference to the bound instance.

Note: Because the reload hooks are called without arguments, it will not work with unbound methods or classmethods.

Changes

This section contains descriptions of changes in each new version.

3.2.0

  • Adds PYCONFIG_ETCD_PROTOCOL and PYCONFIG_ETCD_AUTH.

Released August 17, 2017.

3.1.1

  • Documentation fixes that makes rendering work on PyPI and GitHub again.

Released June 16, 2016.

3.1.0

  • Adds the ability to watch etcd for changes to values. This can be enabled by setting the environment variable PYCONFIG_ETCD_WATCH=true.

Released June 3, 2016.

3.0.2

  • Fixes an issue when using Python 3 compatibility in Python 2.7 and PyOpenSSL.

Released September 28, 2015.

3.0.1

  • Changes the default inherit depth to 2, which is more useful than 1.

3.0.0

  • Adds support for loading configurations from etcd, with inheritance.
  • Use pytool.lang.Namespace instead of alternate implementation.
  • Drops support for Python 2.6 and 3.2.
  • Pyconfig setting keys are now case insensitive by default (Use pyconfig.set('pyconfig.case_sensitive', True) to change the behavior)
  • Adds new clear() method for wiping out the cached configuration.

Older Versions

2.2.1

  • The command line tool will now attempt to handle source files which specify a non-ascii encoding gracefully.

2.2.0

  • Add allow_default keyword option to get() and setting(). Thanks to yarbelk!

2.1.5

  • Fix regression where localconfig.py wasn't being loaded on Python 2.7 due to a logic flow error. Whoops!

2.1.4

  • Broke Python 2.6 in 2.1.1, fixed again.

2.1.2-2.1.3

  • Package clean up and fixing README to work on PyPI again.

2.1.1

  • Fix bug that would break on Python 2.6 and 2.7 when using a localconfig.py.

2.1.0

  • Pyconfig now works on Python 3, thanks to hfalcic!

2.0.0

  • Pyconfig now has the ability to show you what config keys are defined in a directory.

1.2.0

  • No longer uses Python 2.7 format(). Should work on 2.6 and maybe earlier.

1.1.2

  • Move version string into pyconfig.__version__

1.1.1

  • Fix bug with setup.py that prevented installation

1.1.0

  • Allow for implicitly nesting Namespaces when accessing attributes that are undefined

Contributors

Metadata

Release files for pyconfig 3.4.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 pyconfig 3.4.0
File Size Uploaded Provenance Actions
pyconfig-3.4.0.tar.gz 30.7 kB With provenance Details for pyconfig-3.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 50.7 kB

Release files / pyconfig-3.4.0.tar.gz

Download URL pyconfig-3.4.0.tar.gz
Size 30.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d228b1fb2bce3085bc29144108d5595e0fdedab9317835cd80adcba865f4e539
BLAKE2b-256 checksum
How to use checksums
0563f4ceb3161b578bdfd5b019ffebc58c679ccca6e423adb799fd949ce75601
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Release files / pyconfig-3.4.0-py3-none-any.whl

Download URL pyconfig-3.4.0-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd4a4de17969951b0ea1e5b8cbfd20cd4ebc8c619222fe0b455a03d28d5c146e
BLAKE2b-256 checksum
How to use checksums
b0729946067946f4b0e7779c6909d798f3daff361bbd8d2898a4450c3272dc12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Security for pyconfig 3.4.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pyconfig-3.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

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.

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