{ "info": { "author": "Brett Langdon", "author_email": "me@brett.is", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3" ], "description": "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\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/brettlangdon/importhook", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "importhook", "package_url": "https://pypi.org/project/importhook/", "platform": "any", "project_url": "https://pypi.org/project/importhook/", "project_urls": { "Homepage": "https://github.com/brettlangdon/importhook" }, "release_url": "https://pypi.org/project/importhook/1.0.6/", "requires_dist": null, "requires_python": "", "summary": "Execute code when certain modules are imported", "version": "1.0.6" }, "last_serial": 4207376, "releases": { "1.0.4": [ { "comment_text": "", "digests": { "md5": "83b730b759e062510cad3fcaf211ca78", "sha256": "2b31547faa52da68bb41d7a3b50230c0a6a95119674fc5bffede93027cc48a83" }, "downloads": -1, "filename": "importhook-1.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "83b730b759e062510cad3fcaf211ca78", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6550, "upload_time": "2018-08-25T23:05:37", "url": "https://files.pythonhosted.org/packages/e2/3d/af1f3ae3fccf742e3bdf17bbef64267682b32ae5b733025760029bb28479/importhook-1.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "a1de19b886d9721c59ab26b27210f70d", "sha256": "3994b90d60453ffae3a95ac154f09d768ec27664411523e6b9690d412e9d2789" }, "downloads": -1, "filename": "importhook-1.0.4.tar.gz", "has_sig": false, "md5_digest": "a1de19b886d9721c59ab26b27210f70d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5779, "upload_time": "2018-08-25T23:05:38", "url": "https://files.pythonhosted.org/packages/8c/41/5080c98df05a42ce814d668da1b2fe68d781f74de6eb45e3a1cd44b1b4f4/importhook-1.0.4.tar.gz" } ], "1.0.5": [ { "comment_text": "", "digests": { "md5": "b487cf2658deee95aee7568bdbb3e180", "sha256": "548af7af0bd3974f5a3b43366b46ee9da4c43a300c7d3a9f9a0e463af17aff56" }, "downloads": -1, "filename": "importhook-1.0.5-py3-none-any.whl", "has_sig": false, "md5_digest": "b487cf2658deee95aee7568bdbb3e180", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6353, "upload_time": "2018-08-25T23:11:33", "url": "https://files.pythonhosted.org/packages/39/62/b4ed7b98357badb17befda9656bf3938d79ec8afe79f1e658908a7d7b04a/importhook-1.0.5-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "334f2a5e07af4f02bab1150d9d527b93", "sha256": "cd86a3e31b13b3b7cd8bb5c64cb90642378e1d8d97833ff1c6e0c243418e2549" }, "downloads": -1, "filename": "importhook-1.0.5.tar.gz", "has_sig": false, "md5_digest": "334f2a5e07af4f02bab1150d9d527b93", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5605, "upload_time": "2018-08-25T23:11:34", "url": "https://files.pythonhosted.org/packages/f4/21/d36f09a6b951217452c45752d17693a530ee61c112a9337a6995cd9c22ed/importhook-1.0.5.tar.gz" } ], "1.0.6": [ { "comment_text": "", "digests": { "md5": "5feba1f95f6571708d027633fb35dd51", "sha256": "bffb9d216eadb660f99ce680539147f7d87f78162a19219781a0e43308d40f48" }, "downloads": -1, "filename": "importhook-1.0.6-py3-none-any.whl", "has_sig": false, "md5_digest": "5feba1f95f6571708d027633fb35dd51", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6667, "upload_time": "2018-08-25T23:45:42", "url": "https://files.pythonhosted.org/packages/d5/6a/992ceb20b3bbd228765758f37939630f15a7f254572aa2d8f7d2b21c9e89/importhook-1.0.6-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "a023eb4f867056d3d0bbd13e2eed5004", "sha256": "04b20acaec8330b423762d4f2a826c5e658e5093f14aad1740a9b82a433e597e" }, "downloads": -1, "filename": "importhook-1.0.6.tar.gz", "has_sig": false, "md5_digest": "a023eb4f867056d3d0bbd13e2eed5004", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5985, "upload_time": "2018-08-25T23:45:43", "url": "https://files.pythonhosted.org/packages/57/82/92c7c7f307fce321213c27a7af0addc582de78cef4743b6da13518471f0e/importhook-1.0.6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "5feba1f95f6571708d027633fb35dd51", "sha256": "bffb9d216eadb660f99ce680539147f7d87f78162a19219781a0e43308d40f48" }, "downloads": -1, "filename": "importhook-1.0.6-py3-none-any.whl", "has_sig": false, "md5_digest": "5feba1f95f6571708d027633fb35dd51", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6667, "upload_time": "2018-08-25T23:45:42", "url": "https://files.pythonhosted.org/packages/d5/6a/992ceb20b3bbd228765758f37939630f15a7f254572aa2d8f7d2b21c9e89/importhook-1.0.6-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "a023eb4f867056d3d0bbd13e2eed5004", "sha256": "04b20acaec8330b423762d4f2a826c5e658e5093f14aad1740a9b82a433e597e" }, "downloads": -1, "filename": "importhook-1.0.6.tar.gz", "has_sig": false, "md5_digest": "a023eb4f867056d3d0bbd13e2eed5004", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5985, "upload_time": "2018-08-25T23:45:43", "url": "https://files.pythonhosted.org/packages/57/82/92c7c7f307fce321213c27a7af0addc582de78cef4743b6da13518471f0e/importhook-1.0.6.tar.gz" } ] }