PyIV Documentation

A Guice-style dependency injection library for Python. Register bindings, inject by type, and swap production implementations for test doubles without mocking the world.

1.1.0

Zero runtime dependencies. Python 3.8+.

Quick Links: GitHub PyPI Changelog README

Key Features

Type-based injection

Constructor injection from type annotations. Register an interface once; the injector builds the graph.

Scopes

Per-injector and process-wide singletons, plus an extensible Scope API for custom lifecycles.

Keys and Binder

Qualified bindings with tag sets (Named), nearest-match inject (Matched), Annotated constructor params, version-conditioned bindings (including optional package present/absent), and a fluent Binder API.

Reflection

Discover interface implementations in a package instead of listing every class by hand.

Use-case stdlib doubles

Injectable files (mount/zip/tar), time, console, logging, process/env, entropy, and HTTP — swap Real for Memory without patching builtins.

Zero dependencies

Runtime is the Python standard library only. No extra packages to pin or audit.

Installation

pip install pyiv

Requires Python 3.8 or newer. Unreleased main:

pip install git+https://github.com/rl337/pyiv.git

YAML and requests integrations are a second package (core stays stdlib-only):

pip install pyiv-common

See Packages.

Quick Start

from pyiv import Config, get_injector

class Database:
    pass

class PostgreSQL(Database):
    pass

class Logger:
    pass

class FileLogger(Logger):
    pass

class MyConfig(Config):
    def configure(self):
        self.register(Database, PostgreSQL)
        self.register(Logger, FileLogger, singleton=True)

injector = get_injector(MyConfig)
db = injector.inject(Database)       # PostgreSQL
logger = injector.inject(Logger)     # shared per injector

User Guide

Binder vs register, scopes, qualified keys, version-conditioned bindings (Python / package version / package present or absent), and swapping Clock / Filesystem / Console / DateTimeService in tests:

API Reference

The left navigation lists every public module. Pages are generated from docstrings in the pyiv package; doctests in those docstrings are run in CI.

Integrations

Indices and tables