Created
November 24, 2018 16:09
-
-
Save dhepper/37e43a6754b6ed69b9399b2c204e888a to your computer and use it in GitHub Desktop.
A module that patches __import__ to print some information about imported modules.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import builtins | |
builtin_import = builtins.__import__ | |
paths = set() | |
def __import__(*args, **kwargs): | |
module = builtin_import(*args, **kwargs) | |
name = getattr(module, "__name__", "unnamed") | |
version = getattr(module, "__version__", "unknown") | |
path = getattr(module, "__file__", "unknown") | |
if not path in paths: | |
print(f"Imported module {name} <{version}> from {path}") | |
paths.add(path) | |
return module | |
builtins.__import__ = __import__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment