surrogate
raw JSON → 0.1 verified Mon Apr 27 auth: no python
A Python micro-library to create stubs for non-existing modules, enabling testing of import-dependent code. Current version 0.1, no active development observed.
pip install surrogate Common errors
error AttributeError: module 'surrogate' has no attribute 'replace' ↓
cause Using an older version of surrogate where the function was named differently.
fix
Ensure you are using version 0.1 and call surrogate.replace().
error ImportError after surrogate.replace() ↓
cause The module was already imported before calling surrogate.replace().
fix
Ensure surrogate.replace() is called before any imports of the module.
Warnings
gotcha surrogate.replace() must be called before importing the target module, otherwise the real module will be cached. ↓
fix Call surrogate.replace('module_name') before any import of that module.
gotcha Stubs are not full mocks; they only prevent ImportError. Attributes accessed on the stub will raise AttributeError unless defined. ↓
fix If you need attributes, assign them manually after creating the stub.
Imports
- surrogate
import surrogate
Quickstart
import surrogate
# Create a stub for a non-existing module
surrogate.replace('mymodule')
# Now you can import it without ImportError
import mymodule
# Check that it's indeed a stub
print(isinstance(mymodule, surrogate.Surrogate))
# Automatic cleanup on exit