{"id":16584502,"url":"https://github.com/brettlangdon/importhook","last_synced_at":"2025-03-16T21:30:24.871Z","repository":{"id":44877387,"uuid":"145985189","full_name":"brettlangdon/importhook","owner":"brettlangdon","description":"Python package for executing functions when packages are imported","archived":false,"fork":false,"pushed_at":"2023-04-14T22:22:00.000Z","size":203,"stargazers_count":25,"open_issues_count":4,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-27T13:44:47.072Z","etag":null,"topics":["hooks","import","module","python","python3"],"latest_commit_sha":null,"homepage":"https://brettlangdon.github.io/importhook/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brettlangdon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-24T11:50:49.000Z","updated_at":"2024-03-29T11:52:48.000Z","dependencies_parsed_at":"2022-08-25T15:41:08.337Z","dependency_job_id":null,"html_url":"https://github.com/brettlangdon/importhook","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettlangdon%2Fimporthook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettlangdon%2Fimporthook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettlangdon%2Fimporthook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettlangdon%2Fimporthook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brettlangdon","download_url":"https://codeload.github.com/brettlangdon/importhook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243830922,"owners_count":20354850,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","import","module","python","python3"],"created_at":"2024-10-11T22:44:48.415Z","updated_at":"2025-03-16T21:30:24.503Z","avatar_url":"https://github.com/brettlangdon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Importhook\n=========\n\n[![PyPI version](https://badge.fury.io/py/importhook.svg)](https://badge.fury.io/py/importhook)\n\n`importhook` is a Python package that lets you configure functions to call whenever a specific module is imported.\n\n\n## Installation\n\n```bash\npip install importhook\n```\n\n## Usage\nConfigure a hook to be called when `socket` module is imported.\n\n```python\nimport importhook\n\n# Setup hook to be called any time the `socket` module is imported and loaded into module cache\n@importhook.on_import('socket')\ndef on_socket_import(socket):\n    print('\"socket\" module has been imported')\n\n# Import module\nimport socket\n```\n\n\nYou can also use `importhook` to intercept and modify a module on import by returning a Python module from your hook function.\n\n```python\nimport importhook\n\n# Setup hook to be called any time the `socket` module is imported and loaded into module cache\n@importhook.on_import('socket')\ndef on_socket_import(socket):\n    new_socket = importhook.copy_module(socket)\n    setattr(new_socket, 'gethostname', lambda: 'patched-hostname')\n    return new_socket\n\n# Import module\nimport socket\n\n# Prints: 'patched-hostname'\nprint(socket.gethostname())\n```\n\n\n`importhook` also comes with helpers to reload modules that have already been imported.\n\n```python\nimport socket\nimport importhook\n\n\n# Setup hook to be called any time the `socket` module is imported and loaded into module cache\n# DEV: `on_socket_import` will be called immediately because the `socket` module is already loaded\n@importhook.on_import('socket')\ndef on_socket_import(socket):\n    print('\"socket\" module has been imported')\n\n\n# Reload the socket module\n# DEV: Reassign to `socket` in case one of our hooks modifies the module\nsocket = importhook.reload_module(socket)\n```\n## Design decisions\n### Overwriting sys.meta_path\nIf a Python developer wants to modify the import behavior they can do so by adding a new `importlib.abc.Finder`\nclass into `sys.meta_path`.\n\n```python\nimport sys\n\n# Add our custom `importlib.abc.Finder` to `sys.meta_path`\nsys.meta_path.append(MyCustomFinder)\n```\n\nOne of the major design decisions we have taken with `importhook` is to wrap/overwrite `sys.meta_path`.\nWhat it means is that `importhook` will continue to work as expected regardless of any other modifications of `sys.meta_path`.\n\nThere is however one caveat, if you were to do `sys.meta_path = [MyCustomFinder] + sys.meta_path` then `sys.meta_path` will get\nconverted back into a `list`. Existing modifications to the finders in `sys.meta_path` will still work as expected, but any\nnew finders added will not get hooked.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrettlangdon%2Fimporthook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrettlangdon%2Fimporthook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrettlangdon%2Fimporthook/lists"}