from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('bacon.py')
print('Loaded modules:')
for name, mod in finder.modules.items():
print('%s: ' % name, end='')
print(','.join(list(mod.globalnames.keys())[:3]))
print('-'*50)
print('Modules not imported:')
print('\n'.join(finder.badmodules.keys()))
看 module 自身路径用
__file__
:https://docs.python.org/3/reference/import.html?highlight=__file__#import-related-module-attributes
看 module 引用了哪些别的 module 则需要写代码了:
https://docs.python.org/3/library/modulefinder.html
例子摘抄如下:
还可以参考这个:http://furius.ca/snakefood/