Skip to main content

Execute code when certain modules are imported

Project description

Importhook

PyPI version

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

importhook-1.0.5.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

importhook-1.0.5-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file importhook-1.0.5.tar.gz.

File metadata

  • Download URL: importhook-1.0.5.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for importhook-1.0.5.tar.gz
Algorithm Hash digest
SHA256 cd86a3e31b13b3b7cd8bb5c64cb90642378e1d8d97833ff1c6e0c243418e2549
MD5 334f2a5e07af4f02bab1150d9d527b93
BLAKE2b-256 f421d36f09a6b951217452c45752d17693a530ee61c112a9337a6995cd9c22ed

See more details on using hashes here.

File details

Details for the file importhook-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: importhook-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for importhook-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 548af7af0bd3974f5a3b43366b46ee9da4c43a300c7d3a9f9a0e463af17aff56
MD5 b487cf2658deee95aee7568bdbb3e180
BLAKE2b-256 3962b4ed7b98357badb17befda9656bf3938d79ec8afe79f1e658908a7d7b04a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page