Skip to main content

Easy configs for python projects

Project description

P-Conf

Easy configuration for Python projects. Load TOML, JSON, INI, or YAML from an explicit path, from OS-specific application directories (for example ~/.config/<appname>/config.toml), or directly from a Python dict, then read values with attribute access (cfg.section.key). You can use the built-in singleton, pass Config objects around, or integrate with Pydantic via ConfigSettings.

Where config files are searched

By default, discovery checks:

  • ~/.config/<appname>/.config.toml
  • ~/.config/<appname>/config.(toml|json|ini|yaml)
  • <system config directory>/<appname>/.config.toml
  • <system config directory>/<appname>/config.(toml|json|ini|yaml)

YAML requires the optional extra, for example: uv add "pi-conf[yaml]".

Quick start

Example config.toml:

foo = 1
[bar]
a = 2

Single script

from pi_conf import load_config

cfg = load_config(appname="ourappname")
print(cfg.foo)    # 1
print(cfg.bar.a)  # 2

Application package

Set config once (for example in __init__.py), then import cfg elsewhere:

# __init__.py
from pi_conf import cfg, set_config

set_config("ourappname")
# other_module.py
from ourappname import cfg
print(cfg.foo)

Each load_config(...) call returns a new Config. To replace the shared module-level cfg, use set_config(...).

Explicit sources (recommended for clarity)

load_config(...) supports exactly one explicit source keyword, and these are usually clearer than the legacy positional argument:

load_config(data={"debug": True})
load_config(path="/etc/myapp/config.toml")
load_config(appname="myapp")
load_config(appname="myapp", file="secrets.toml")  # not `config.<ext>`

Do not mix these keywords with each other, or with a positional source, in the same call.

Other helpers

  • AttrDict / Config: nested dicts with attribute access; Config adds optional provenance tracking.
  • Config.load_config(...): merge another config source into an existing Config; conflicting top-level keys raise by default, or pass overwrite=True to replace them.
  • get_nested("a.b.c"): dot-path access, with optional defaults and list indexing (see tests in tests/test_nested_get.py).
  • AttrDict.from_str(text, "toml"|"json"|"ini"|"yaml"): parse config from a string.
  • to_env(): export nested config to environment variables (see tests/test_config.py).

Pydantic (ConfigSettings)

Install with your environment’s equivalent of:

uv sync --extra pydantic
# or ensure pydantic-settings and toml are available for TOML round-trips on Python 3.11+

Typical TOML usage is to define a subclass and point it at either an explicit file path or an app name:

from pi_conf import ConfigDict, ConfigSettings


class Settings(ConfigSettings):
    host: str
    port: int

    model_config = ConfigDict(toml_file="/etc/myapp/config.toml")
from pi_conf import ConfigDict, ConfigSettings


class Settings(ConfigSettings):
    host: str
    port: int

    model_config = ConfigDict(appname="myapp")

You can also load a regular Pydantic model from config with ConfigSettings.from_config(...).

For nested TOML sections, set toml_table_header. For MongoDB-backed settings, use mongo_uri, mongo_database, mongo_collection, and mongo_query when pymongo is installed. See tests/test_config_settings.py and tests/test_config_settings_mongo.py.

ConfigSettings loads without importing bson unless MongoDB support is used. Updating TOML on disk uses the PyPI toml package (declared with the pydantic dependency group) because the stdlib tomllib is read-only.

Development

  • Install the development environment: uv sync --group dev --all-extras
  • Run tests: make test or uv run pytest tests
  • CI runs on Python 3.9, 3.11, and 3.13 (see .github/workflows/ci.yml).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pi_conf-1.0.0.tar.gz (57.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pi_conf-1.0.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file pi_conf-1.0.0.tar.gz.

File metadata

  • Download URL: pi_conf-1.0.0.tar.gz
  • Upload date:
  • Size: 57.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for pi_conf-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d5f75b1111822d9652fcf4914a3ed3945d449fed629a98a53033a26821a59d0e
MD5 2fb98e384c9762a22f5543a718f8e2dc
BLAKE2b-256 a1a9d583bfc4533b6487a40824e11f1783d77dc5bc128e1c85d5dd516c28c29c

See more details on using hashes here.

File details

Details for the file pi_conf-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pi_conf-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for pi_conf-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34ed8e9d3ff7cef3b3020ecc5d5cd74475bbb4ae7d151705186eb4a5e0be79ab
MD5 17d76b71588f7e5a7e7a1153d4f0fed6
BLAKE2b-256 eed324550bee84736f4eb011b1aa68cdebcee85e3cdd56285db8c3469f5bff52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page