Skip to main content

importpatch

Version Python versions License: MIT Docs CI

Post-import hooks that a package registers just by being installed. A distribution declares "when requests is imported, call mypkg.hooks.patch" as an entry point, and every program in that environment gets the patch the moment it imports requests, without importing mypkg itself. A hook that fails is logged and skipped; it never breaks the import it patches. Documentation: https://jose-pr.github.io/importpatch/.

Features

  • Registration by entry point — the importpatch group: name = the module to patch, value = the patcher's dotted name. Uninstalling the package removes its hooks.
  • Active on install — the wheel ships importpatch.pth, so the hook runs at the start of every interpreter in the environment; IMPORTPATCH_DISABLE=1 turns it off.
  • Works with any loader — the module is found by Python's own finders and its loader is wrapped, so packages, zip archives and anything else importable are patched.
  • Already imported is fine — a module imported before its hook was registered is patched at registration.
  • Fail-safe — a hook that is missing or raises is logged on the importpatch logger and skipped.
  • Cheap — no dependencies, and entry points are read without importlib.metadata, which would cost every interpreter start tens of milliseconds.

Installation

pip install importpatch

There are no extras.

Quick start

A package that gives every requests call a default timeout declares, in its pyproject.toml:

[project.entry-points.importpatch]
requests = "mypkg.hooks.patch_requests"

and in mypkg/hooks.py:

def patch_requests(requests):
    original = requests.Session.request

    def request(self, method, url, **kwargs):
        kwargs.setdefault("timeout", 30)
        return original(self, method, url, **kwargs)

    requests.Session.request = request

With both installed, every program in the environment that imports requests gets the patched Session.request. Registering in-process needs no packaging:

import importpatch

importpatch.add("json", "mypkg.hooks.patch_json")  # runs at once if json is loaded

API overview

Name Purpose
importpatch.add(target, patcher) run patcher on target after it is imported (at once, if it is)
importpatch.remove(target, patcher=None) stop running one or every patcher on future imports
importpatch.patches() the registry: target → patchers
importpatch.bootstrap() install the hook and read entry points and the configuration file (importpatch.pth calls it)

Configuration file

pyimportpatch.cfg adds hooks that do not come from a package, and switches hooks off. It has one section per module to patch, then one patcher per line:

# lines starting with # or ; are comments
[json]
mypkg.hooks.patch_json

[requests]
otherpkg.hooks.patch_requests = false

A patcher alone, or = true, enables it; = false disables it, whether a package's entry point, an earlier file or the same file registered it (booleans: 1/0, true/false, yes/no, on/off).

A patcher is a dotted name. When it names a function, the function is called with the imported module: patch_json(module). When it names a module, that module's importpatch(name, module) is called with the imported module's name and the module, so one module can patch several targets. The entry-point spelling pkg.mod:function is the same patcher as pkg.mod.function.

Two files are read and merged, after the entry points, each overriding what came before it:

File Windows Elsewhere Override
system %PROGRAMDATA%\pyimportpatch.cfg /etc/pyimportpatch.cfg IMPORTPATCH_SYSTEM_CONFIG
user %APPDATA%\pyimportpatch.cfg $XDG_CONFIG_HOME/pyimportpatch.cfg (default ~/.config) IMPORTPATCH_CONFIG

Each variable may list several files separated by os.pathsep (:, or ; on Windows), applied in order. The user files are not read under python -I or -s; nothing is read from the working directory. A line that cannot be read is logged with the file and line number, and skipped.

Migrating from 0.1

  • There is no python -m importpatch enable / disable: installing activates the hook, uninstalling removes it, and IMPORTPATCH_DISABLE=1 turns it off. Installing 0.2 replaces the importpatch.pth that enable wrote.
  • .pymodpatch.json in site-packages, ~ or the working directory is no longer read; move its entries to pyimportpatch.cfg (one [module] section each), or declare entry points.
  • rm() is remove(); MODULES_TO_PATCH is private (use patches()).
  • A hook that raises or cannot be imported is logged, instead of being ignored silently or, for a missing package, failing the import.

Development

In a virtual environment on Python 3.9 or later:

python -m pip install -e ".[dev]"
python -m pytest -q -W error

The install tests build the wheel and run real interpreters in a temporary virtual environment.

Releasing

This project follows Semantic Versioning and keeps a CHANGELOG.md. Pushing a tag matching v* triggers the release workflow: test gate → build (checking the tag names the version built) → a strict docs build as a gate → GitHub release → publish. The release workflow never deploys the docs site itself: for a final release its last job dispatches the docs workflow at the tag, which owns every Pages deploy.

License

MIT — see LICENSE.

Metadata

Release files for importpatch 0.2.1

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

Source distribution (sdist)

Source distribution for importpatch 0.2.1
File Size Uploaded Provenance
importpatch-0.2.1.tar.gz 17.7 kB With provenance Details

Built distribution (wheel)

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

Total release size: 29.9 kB

Release files / importpatch-0.2.1.tar.gz

Download URL importpatch-0.2.1.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f68c76e679b97af95855117c770ed852f428ca137ad3027ad365385cc3d9d779
BLAKE2b-256 checksum
How to use checksums
179487fb8ef670b50f9aaa1bdb1319fc57e6afae348097c7e61918629875014c
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 Oct 19, 2026.

Transparency log

Release files / importpatch-0.2.1-py3-none-any.whl

Download URL importpatch-0.2.1-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9860c43e120ef6ee0eb5f181818e2d9d72fa721d4c6c7bb05601a8e8be8ae6cf
BLAKE2b-256 checksum
How to use checksums
67a07261682eb2f29614c417dd0f391c2001313342368d77209dae5025cf368f
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 Oct 19, 2026.

Transparency log

Security for importpatch 0.2.1

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for importpatch-0.2.1.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 Oct 19, 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