Autowrapt
Autowrapt is a Python library designed to provide a bootstrap mechanism for applying monkey patches to applications without requiring direct modification of the application's source code. It achieves this by working in conjunction with the `wrapt` module and leveraging setuptools entry points, activated via the `AUTOWRAPT_BOOTSTRAP` environment variable. The current version is 1.0, released in 2015, and it functions as a stable utility in maintenance mode, with its core dependency `wrapt` being actively maintained.
Common errors
-
Monkey patch specified in AUTOWRAPT_BOOTSTRAP is not applied or has no effect.
cause The most common causes are an incorrect installation of `autowrapt` (not using pip), a misspelled or incorrect entry point name in the `AUTOWRAPT_BOOTSTRAP` environment variable, or the target module being imported before `autowrapt`'s bootstrap mechanism can activate.fixEnsure `autowrapt` was installed with `pip`. Verify that the `AUTOWRAPT_BOOTSTRAP` environment variable exactly matches the `setuptools` entry point name for your patch. Check the timing of module imports in your application; autowrapt aims to run early, but explicit `from module import function` statements before bootstrap can prevent patches from taking effect on those specific references. -
Error: 'No module named autowrapt.bootstrap' or similar during Python interpreter startup.
cause This error can occur if the `autowrapt-init.pth` file is not correctly placed or processed by the Python interpreter, which is often a symptom of `autowrapt` not being installed via `pip`.fixReinstall `autowrapt` using `pip` (`pip install autowrapt`). Avoid `python setup.py install` or `easy_install`. Confirm that `autowrapt-init.pth` exists in your Python's `site-packages` directory.
Warnings
- breaking Incorrect installation method can prevent autowrapt from working. Using `python setup.py install` or `easy_install` can cause the critical `.pth` file to be installed incorrectly (e.g., inside an egg directory), preventing activation.
- gotcha Monkey patching, especially using `.pth` files, is a powerful technique that runs code early in the Python interpreter's startup. This can introduce hard-to-debug issues related to import order, unexpected side effects, and security concerns as code runs implicitly.
- gotcha The `autowrapt` project itself has not been updated since 2015 (version 1.0). While its core dependency `wrapt` is actively maintained, `autowrapt` may not fully leverage the latest features or address edge cases in newer Python versions or `wrapt` versions beyond what was available in 2015.
Install
-
pip install autowrapt
Imports
- autowrapt.bootstrap
N/A (activated via env var)
Quickstart
import this