首页 > 开发 > NodeJS > 正文

python3 如何获取模块的自身路径以及依赖的路径列表

2017-09-08 17:34:07  来源:网友分享

相当于nodejs的

module.filenamemodule.children

解决方案

看 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

例子摘抄如下:

from modulefinder import ModuleFinderfinder = 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()))

还可以参考这个:http://furius.ca/snakefood/