Python:模块的完整列表

2024-04-19 06:02:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如何获取所有可用python模块的列表?

我不想要模块化的模块。只是sys.path中所有可用模块的基本列表。

help('modules')不是解决方案,因为我希望它作为变量可用,并导入那些有副作用的模块。

编辑:有了副作用,我指的是像kivy of http://kivy.org/这样的库,它们利用了这个事实,一旦导入代码,就会执行它。


Tags: 模块ofpathorgmoduleshttp编辑利用
2条回答

pkgutil-支持包的实用程序

这将为sys.path上的所有子模块生成一个元组:

pkgutil.iter_modules()

要查看加载的内容,请查看:

sys.modules

"This is a dictionary that maps module names to modules which have already been loaded"

加载的模块列表:

sys.modules.keys() 

使用Python安装附带的外部脚本“pydoc”: 在命令shell中,键入:

$ pydoc modules

Pydoc也可以在Python中使用,让它遍历所有可用内容的一种方法是:

all_mod = []
pydoc.ModuleScanner().run(callback=(lambda *a: all_mod.append(a[1])), onerror=lambda *a:None)
print all_mod

相关问题 更多 >