Skip to content

Instantly share code, notes, and snippets.

@dhepper
Created November 24, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhepper/37e43a6754b6ed69b9399b2c204e888a to your computer and use it in GitHub Desktop.
Save dhepper/37e43a6754b6ed69b9399b2c204e888a to your computer and use it in GitHub Desktop.
A module that patches __import__ to print some information about imported modules.
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