import sys
sys.modules.keys()
An approximation of getting all imports for the current module only would be to inspect globals()
for modules:
import types
def imports():
for name, val in globals().items():
if isinstance(val, types.ModuleType):
yield val.__name__
This won't return local imports, or non-module imports like from x import y
. Note that this returns val.__name__
so you get the original module name if you used import module as alias
; yield name instead if you want the alias.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…