Skip to main content

configreader

Python library to read configuration values from multiple sources with configurable precedence.

Supported sources:

  • INI file
  • SQL database through SQLAlchemy (optional)
  • environment variables
  • in-memory Python dictionary

Values are resolved in order, and the first non-empty match is returned.

Installation

From PyPI

pip install ga-configreader

From source

git clone https://github.com/andreagemma/configreader.git
cd configreader
pip install -e .

Database support (optional)

Install SQLAlchemy if you want to use the DB provider:

pip install sqlalchemy

Or install the project with DB extras:

pip install "configreader[db]"

Quickstart

from configreader import ConfigReader

reader = ConfigReader(
    file="config.ini",
    use_env=True,
    env_default_section="APP",
    dictionary={"DEFAULT": {"timeout": "30"}},
    providers=["env", "ini", "dict"],
    cached=True,
)

host = reader.get("host", default="127.0.0.1")
port = reader.getint("port", default=8080)
debug = reader.getboolean("debug", default=False)

Provider Precedence

The providers list defines lookup order.

Example:

providers = ["env", "db", "ini", "dict"]

Meaning:

  1. check environment first
  2. then check database
  3. then check INI file
  4. then check dictionary

Environment Variables

Naming rules:

  • environment variables are read as SECTION_NAME, using the requested section name as prefix
  • with the default section, this means DEFAULT_NAME unless you set env_default_section or pass an empty section

Examples:

  • reader.get("host") reads DEFAULT_HOST
  • reader.get("host", section="app") reads APP_HOST
  • ConfigReader(env_default_section="APP").get("host") reads APP_HOST
  • reader.get("host", section="") reads HOST first, then DEFAULT_HOST

Using INI Files

Example config.ini:

[DEFAULT]
host = localhost
port = 5432
debug = true
items = [1, 2, 3]

[app]
workers = 4

Code:

reader = ConfigReader(file="config.ini")

host = reader.get("host")
port = reader.getint("port")
debug = reader.getboolean("debug")
items = reader.getlist("items")
workers = reader.getint("workers", section="app")

Using a Dictionary

reader = ConfigReader(
    dictionary={
        "DEFAULT": {
            "host": "localhost",
            "allowed": "['admin', 'user']",
        },
        "service": {
            "retries": "3",
        },
    },
    providers=["dict"],
)

allowed = reader.getlist("allowed")
retries = reader.getint("retries", section="service")

Using a Database

Constructor:

reader = ConfigReader(
    db_url="sqlite:///settings.db",
    db_query="SELECT value FROM settings WHERE section = :section AND name = :name",
    providers=["db", "env"],
)

Default query shape:

SELECT value FROM settings WHERE section = :section AND name = :name

DB utility methods:

ok = ConfigReader.check_db_connection("sqlite:///settings.db")
exists = ConfigReader.check_db_exists("sqlite:///settings.db", table_name="settings")

Main API

  • get(name, default=None, section="DEFAULT") -> str | None
  • getint(name, default=None, section="DEFAULT") -> int | None
  • getboolean(name, default=None, section="DEFAULT") -> bool | None
  • getfloat(name, default=None, section="DEFAULT") -> float | None
  • getlist(name, default=None, section="DEFAULT") -> list[Any] | None
  • getset(name, default=None, section="DEFAULT") -> set[Any] | None
  • gettuple(name, default=None, section="DEFAULT") -> tuple[Any, ...] | None
  • getdict(name, default=None, section="DEFAULT") -> dict[Any, Any] | None
  • sections() -> list[str] merged section names across enabled providers
  • variables(section) -> list[str] merged variable names for a section across enabled providers
  • items() iterator over loaded INI entries
  • refresh(on_cache_exists="warining") -> None reload providers and rebuild cache
  • cache(on_cache_exists="warining") -> None build cache on demand
  • copy(copy_cache=False, on_cache_exists="warining") -> ConfigReader

Cache Behavior

  • By default cached=True, so values are snapshotted at initialization for enabled providers.
  • With cache enabled, get, items, sections, and variables read from cache.
  • Call refresh() to reload providers and rebuild cache.
  • Use cache() to create cache on demand when cached=False.
  • For refresh, cache, and copy(copy_cache=True), on_cache_exists controls behavior when cache is missing:
    • "ignore": no warning or error
    • "warning": emit RuntimeWarning (default)
    • "raise": raise RuntimeError
  • The default is "warining" for backward compatibility and is normalized to "warning".

Full details in docs/api.md.

Errors And Type Conversion

  • FileNotFoundError is raised if the provided INI file does not exist.
  • Typed getters (getint, getfloat, getlist, etc.) propagate parsing/conversion errors.
  • If SQLAlchemy is not installed, DB features are unavailable.

Development

Install development dependencies:

pip install -e .[dev]

Run tests:

pytest

More Documentation

License

Distributed under the MIT License. See LICENSE.

Third-party dependency notices and archived license files are available in:

Release files for ga-configreader 0.2.6

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

Source distribution (sdist)

Source distribution for ga-configreader 0.2.6
File Size Uploaded
ga_configreader-0.2.6.tar.gz 29.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ga-configreader 0.2.6
File Interpreter ABI Platform
ga_configreader-0.2.6-py3-none-any.whl Python 3 none any Details

Total release size: 41.2 kB

Release files / ga_configreader-0.2.6.tar.gz

Download URL ga_configreader-0.2.6.tar.gz
Size 29.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f40d9b433bdba9992ab2fc850700254076f6cf9a6e2eb65ff7261b6fdcca6e81
BLAKE2b-256 checksum
How to use checksums
09849841d352027e9dfe8ca766e905afc21e8616997a7cecc1740185f6249c84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 24, 2026.

Transparency log

Release files / ga_configreader-0.2.6-py3-none-any.whl

Download URL ga_configreader-0.2.6-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ede7a5ece7538f7d88e106663cc314465bf0bd430dac1bff46a12676e1e294ca
BLAKE2b-256 checksum
How to use checksums
a77f770f1876201bc314825e29ed4f0a0dede445d7ba118d65a5d2106cd1a72e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 24, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.6 This release

2 release files

0.2.3

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.4

2 release files

0.1.3

2 release files

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