libb.patch_module
- patch_module(source_name, target_name)[source]
Replace a source module with a target module in sys.modules.
Useful when writing a module with the same name as a standard library module and needing to import the original.
- Parameters:
- Returns:
The target module.
- Return type:
ModuleType
Example:
>>> import sys >>> original_sys = sys.modules['sys'] >>> _sys = patch_module('sys', '_sys') >>> 'sys' in sys.modules False >>> '_sys' in sys.modules True >>> sys.modules['sys'] = original_sys # Restore original sys module