Importhook
importhook is a Python package that lets you configure functions to call whenever a specific module is imported.
Installation
pip install importhook
Usage
Configure a hook to be called when socket module is imported.
import importhook
# Setup hook to be called any time the `socket` module is imported and loaded into module cache
@importhook.on_import('socket')
def on_socket_import(socket):
print('"socket" module has been imported')
# Import module
import socket
You can also use importhook to intercept and modify a module on import by returning a Python module from your hook function.
import importhook
# Setup hook to be called any time the `socket` module is imported and loaded into module cache
@importhook.on_import('socket')
def on_socket_import(socket):
new_socket = importhook.copy_module(socket)
setattr(new_socket, 'gethostname', lambda: 'patched-hostname')
return new_socket
# Import module
import socket
# Prints: 'patched-hostname'
print(socket.gethostname())
importhook also comes with helpers to reload modules that have already been imported.
import socket
import importhook
# Setup hook to be called any time the `socket` module is imported and loaded into module cache
# DEV: `on_socket_import` will be called immediately because the `socket` module is already loaded
@importhook.on_import('socket')
def on_socket_import(socket):
print('"socket" module has been imported')
# Reload the socket module
# DEV: Reassign to `socket` in case one of our hooks modifies the module
socket = importhook.reload_module(socket)
Design decisions
Overwriting sys.meta_path
If a Python developer wants to modify the import behavior they can do so by adding a new importlib.abc.Finder
class into sys.meta_path.
import sys
# Add our custom `importlib.abc.Finder` to `sys.meta_path`
sys.meta_path.append(MyCustomFinder)
One of the major design decisions we have taken with importhook is to wrap/overwrite sys.meta_path.
What it means is that importhook will continue to work as expected regardless of any other modifications of sys.meta_path.
There is however one caveat, if you were to do sys.meta_path = [MyCustomFinder] + sys.meta_path then sys.meta_path will get
converted back into a list. Existing modifications to the finders in sys.meta_path will still work as expected, but any
new finders added will not get hooked.
Metadata
Release files for importhook 1.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| importhook-1.0.9.tar.gz | 6.3 kB | Without provenance | Details for importhook-1.0.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| importhook-1.0.9-py3-none-any.whl | Python 3 | none | any | Details for importhook-1.0.9-py3-none-any.whl |
Total release size: 14.2 kB
Release files / importhook-1.0.9.tar.gz
| Download URL | importhook-1.0.9.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
635ad367225fa022221025545c167b09d09ada671821b6e587df0d5ae4bf0aab
|
|
BLAKE2b-256 checksum How to use checksums |
da20ba0146cefd926dd6bff3657418b467a3c227af6b4a9c2e1b5fc68d8a1deb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.7.7
|
Release files / importhook-1.0.9-py3-none-any.whl
| Download URL | importhook-1.0.9-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
64500997b59abf054ddd5037c5133879a4b3ee2091370b46af30befd0be331c1
|
|
BLAKE2b-256 checksum How to use checksums |
4276e833efe967ee1e18a54ce6a2e8e58d351c2844fec3afeedb5ea368406f67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.7.7
|
Security for importhook 1.0.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.