|
| 1 | +# Python Import Rewriter |
| 2 | + |
| 3 | +A flexible library for dynamically rewriting Python imports at runtime using metapath hooks. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Import Rewriter allows you to transparently redirect imports in Python code without modifying the source. This is useful for: |
| 8 | + |
| 9 | +- Providing mock implementations during testing |
| 10 | +- Implementing feature toggles or A/B testing at the module level |
| 11 | +- Creating compatibility layers for different library versions |
| 12 | +- Redirecting imports for dependency injection |
| 13 | +- Implementing module aliases for refactoring |
| 14 | +- Vendoring dependencies at runtime |
| 15 | + |
| 16 | +The library works by intercepting the Python import system using metapath hooks and rewriting the import statements in the Abstract Syntax Tree (AST) before execution. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install import-rewriter |
| 22 | +``` |
| 23 | + |
| 24 | +## Quick Start |
| 25 | + |
| 26 | +```python |
| 27 | +from import_rewriter import install_import_rewriter |
| 28 | + |
| 29 | +# Redirect imports of 'legacy_module' to use 'modern_module' instead |
| 30 | +install_import_rewriter({ |
| 31 | + 'legacy_module': 'modern_module', |
| 32 | + 'requests': 'my_custom_requests' |
| 33 | +}) |
| 34 | + |
| 35 | +# Now when your code does: |
| 36 | +import legacy_module |
| 37 | +# It will actually import 'modern_module' |
| 38 | +``` |
| 39 | + |
| 40 | +## Features |
| 41 | + |
| 42 | +- Transparently rewrites imports without changing the original code |
| 43 | +- Handles both `import x` and `from x import y` statements |
| 44 | +- Can be enabled/disabled at runtime |
| 45 | +- Minimal performance impact for non-rewritten imports |
| 46 | +- Works with standard Python imports, no special syntax required |
| 47 | +- Preserves import aliases (`import x as y`) |
| 48 | + |
| 49 | +## API Reference |
| 50 | + |
| 51 | +### `install_import_rewriter(import_map=None)` |
| 52 | + |
| 53 | +Installs the import rewriting hook with the specified mapping. |
| 54 | + |
| 55 | +**Parameters:** |
| 56 | +- `import_map` (dict): A dictionary mapping original import names to replacement names. |
| 57 | + |
| 58 | +**Returns:** |
| 59 | +- The finder instance that was installed (can be used to uninstall later). |
| 60 | + |
| 61 | +**Example:** |
| 62 | +```python |
| 63 | +finder = install_import_rewriter({ |
| 64 | + 'pandas': 'custom_pandas', |
| 65 | + 'tensorflow': 'tensorflow_lite' |
| 66 | +}) |
| 67 | +``` |
| 68 | + |
| 69 | +### Uninstalling the Hook |
| 70 | + |
| 71 | +To remove the hook and restore normal import behavior: |
| 72 | + |
| 73 | +```python |
| 74 | +import sys |
| 75 | +sys.meta_path.remove(finder) |
| 76 | +``` |
| 77 | + |
| 78 | +## Advanced Usage |
| 79 | + |
| 80 | +### Selective Module Rewriting |
| 81 | + |
| 82 | +You can selectively rewrite imports for specific modules or packages: |
| 83 | + |
| 84 | +```python |
| 85 | +install_import_rewriter({ |
| 86 | + 'pandas.DataFrame': 'my_package.DataFrame', |
| 87 | + 'numpy.array': 'my_package.fast_array' |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +### Dynamic Rewriting |
| 92 | + |
| 93 | +You can change the import mapping at runtime: |
| 94 | + |
| 95 | +```python |
| 96 | +finder = install_import_rewriter() |
| 97 | +finder.import_map['requests'] = 'mock_requests' # Add new mapping |
| 98 | +``` |
| 99 | + |
| 100 | +## How It Works |
| 101 | + |
| 102 | +Import Rewriter uses three key components: |
| 103 | + |
| 104 | +1. **MetaPath Finder**: Intercepts import requests before they're processed by the standard import machinery. |
| 105 | +2. **AST Transformer**: Parses and modifies the abstract syntax tree of the source code. |
| 106 | +3. **Custom Loader**: Executes the modified code in the module's namespace. |
| 107 | + |
| 108 | +When a module is imported, the finder intercepts the request, the transformer rewrites any matching imports, and the loader executes the modified code. |
| 109 | + |
| 110 | +## Limitations |
| 111 | + |
| 112 | +- Only rewrites imports in modules that are loaded after the hook is installed |
| 113 | +- Cannot rewrite imports in c-extensions, built-in, or frozen modules |
| 114 | +- May have unpredictable interactions with other import hooks |
| 115 | + |
| 116 | +## License |
| 117 | + |
| 118 | +GPLv3 |
| 119 | + |
| 120 | +## Contributing |
| 121 | + |
| 122 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments