如何访问 Python 的帮助(“模块”)显示的模块列表?
如何访问 Python 的 help('modules')
显示的模块列表?它显示以下内容:
>>> help('modules')
Please wait a moment while I gather a list of all available modules...
...[list of modules]...
MySQLdb codeop mailman_sf spwd
OpenSSL collections markupbase sqlite3
Queue colorsys marshal sre
...[list of modules]...
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".
>>>
我可以查看输出中的列表,但希望从 Python 程序中将其作为列表进行访问。我该怎么做?
How can I access the list of modules that Python's help('modules')
displays? It shows the following:
>>> help('modules')
Please wait a moment while I gather a list of all available modules...
...[list of modules]...
MySQLdb codeop mailman_sf spwd
OpenSSL collections markupbase sqlite3
Queue colorsys marshal sre
...[list of modules]...
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".
>>>
I can view the list in the output but would like to access this as a list from within a Python program. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法不止一种。你可以尝试:
There are more then one way. You could try:
模块列表最终来自 sys.builtin_module_names 和pkgutil.walk_packages 的输出:
它应该请注意,创建列表的结果是所有模块都将被导入(您可以通过在调用
help('modules 之前和之后检查 sys.modules
的长度来轻松验证这一点'))。另一件需要注意的事情是
walk_packages
的输出取决于sys.path
的当前状态 - 因此结果可能并不总是与help
的输出匹配代码>.The list of modules comes ultimately from a combination of sys.builtin_module_names and the output of pkgutil.walk_packages:
It should be noted that creating the list has the consequnce that all the modules will be imported (you can easily verify this for yourself by checking the length of
sys.modules
before and after callinghelp('modules')
).Another thing to note is that the output of
walk_packages
depends on the current state ofsys.path
- so the results may not always match the output ofhelp
.这些只会列出标准库中未包含的模块,
但可能有用,
subprocess.call(
pip 冻结)
subprocess.call(
蛋黄 -l)
Those will only list modules not included in the standard library,
but may be useful,
subprocess.call(
pip freeze)
subprocess.call(
yolk -l)
您可以自己模仿
help
所做的所有操作。内置help
使用pydoc
,它使用ModuleScanner
类来获取有关所有可用库的信息 - 请参阅 pydoc.py。这是链接中代码的稍微修改版本:
You can mimic all the
help
does by yourself. Built-inhelp
usespydoc
, that usesModuleScanner
class to get information about all available libs - see line 1873 in pydoc.py.Here is a bit modified version of code from the link: