Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

autowrapt

Bootstrap mechanism for monkey patches.

autowrapt triggers the monkey patching of a Python application at interpreter startup, without the application itself being modified. It is a small companion to wrapt, whose post import hook mechanism does the actual work. You write the patches as post import hooks, expose them as entry points, and name the entry point group in an environment variable when running the application.

Installation

pip install autowrapt

autowrapt must be installed into the same Python installation or virtual environment as the application it is to patch. Installing it puts a autowrapt-init.pth file at the top of site-packages, which is what triggers it at interpreter startup.

Usage

Write a post import hook for each module you want to patch. It is a function which takes the module as its only argument and is called immediately after the module is first imported.

# mycompany/patches.py

import wrapt


def patch_requests(module):
    @wrapt.wrap_function_wrapper(module, "get")
    def wrapper(wrapped, instance, args, kwargs):
        print("GET", args[0] if args else kwargs.get("url"))

        return wrapped(*args, **kwargs)

Register the hooks as entry points in the package which contains them. The entry point group name is yours to choose, and within it each entry point name is the module the hook is for.

# pyproject.toml

[project.entry-points."mycompany.patches"]
requests = "mycompany.patches:patch_requests"

Run the application with the AUTOWRAPT_BOOTSTRAP environment variable set to the name of the entry point group.

AUTOWRAPT_BOOTSTRAP=mycompany.patches python app.py

More than one group can be named, separated by commas. Every entry point in each group is registered as a post import hook, and each hook runs when, and only when, its module is first imported. A module which is never imported costs nothing. When the variable is not set, nothing from autowrapt is imported at all.

Example

The package ships one example hook, registered under the autowrapt.examples group against the this module.

AUTOWRAPT_BOOTSTRAP=autowrapt.examples python -c "import this"

This prints the Zen of Python as normal, with one extra line added at the end.

How it works

The .pth file is processed by the site module while the interpreter starts up. Its single line imports autowrapt.bootstrap and calls bootstrap(), but only when AUTOWRAPT_BOOTSTRAP is set.

Because .pth files are processed part way through site initialisation, before the module search path is complete, bootstrap() does not register the hooks straight away. It arranges for that to happen as the last step of site initialisation instead, after the sitecustomize module, or the usercustomize module when support for that is enabled, has been loaded. Only then is wrapt imported and asked to discover the entry points in each named group.

Limitations

  • autowrapt has to be installed in the same environment as the application. A wrapper script run from one environment cannot patch a Python program run from another.

  • Nothing happens when Python is run with the -S option, or in an embedded interpreter which does not run the site module, since the .pth file is never processed.

  • Installers which do not honour files at the root of a wheel, or tools which build environments without processing .pth files, will not trigger autowrapt.

Documentation

The post import hook mechanism autowrapt builds on is described in the monkey patching section of the wrapt documentation. Documentation for autowrapt itself is also being added there, since the two are bound up together.

wrapture, a higher level monkey patching, testing and tracing library built on wrapt, uses autowrapt for zero-code injection of its own patches.

License

BSD 2-Clause. See LICENSE.

Metadata

Release files for autowrapt 2.0.0rc1

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

Source distribution (sdist)

Source distribution for autowrapt 2.0.0rc1
File Size Uploaded
autowrapt-2.0.0rc1.tar.gz 63.0 kB Details

Built distribution (wheel)

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

Total release size: 69.7 kB

Release files / autowrapt-2.0.0rc1.tar.gz

Download URL autowrapt-2.0.0rc1.tar.gz
Size 63.0 kB
Tags Source
SHA-256 checksum
How to use checksums
57c62d1f75a3504ac5b86220380095c6b9fd5b39d29679eee5720319a69e6191
BLAKE2b-256 checksum
How to use checksums
6d20c966d564a1f958118eb95ee0cc71dae72bf1cc13e70a7c810284cc2f73c6
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 / autowrapt-2.0.0rc1-py3-none-any.whl

Download URL autowrapt-2.0.0rc1-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f2d4c34321e847002452feb44e078619d25b6a8094fa9cb58445721fd93931df
BLAKE2b-256 checksum
How to use checksums
c82ddf876a17082b90c9c77ca5db6c2975429cef87fa6b24316154ece5308798
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

2.0.0

2 release files

This release

2.0.0rc1 This release

2 release files

1.0

1 release file

0

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