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.
pip install autowraptVerified import paths — ran on the pinned version, not inferred.
Autowrapt applies patches via environment variables and setuptools entry points. To quickly test, first define a patch in a module (e.g., `my_patch.py` with `def add_zen_line(module): print('The wrapt package is absolutely amazing and you should use it.')`). Then, in a `setup.py` in the same directory, register this as an entry point: `entry_points={'autowrapt.examples': ['my_patch = my_patch:add_zen_line']}`. Install your package in editable mode (`pip install -e .`). Finally, run your Python script with the `AUTOWRAPT_BOOTSTRAP` environment variable set to activate your patch. This example demonstrates patching Python's `this` module to add an extra line to the Zen of Python.
Always install `autowrapt` using `pip` (e.g., `pip install autowrapt` or `pip install -e .` for development).
Thoroughly test patched applications. Be aware of the 'ordering problem' where other code might import a module before the patch can be applied. Consider explicit patching with `wrapt` if implicit global patching is not strictly necessary. For security, be cautious about installing packages that leverage `.pth` files.
Review the GitHub repository for any open issues or discussions related to compatibility with modern Python versions (e.g., Python 3.9+). Consider whether its specific approach is still the best fit for current project needs or if manual `wrapt` usage or other patching techniques are more appropriate.