[英]How can I print Python's help page straight to stdout?
I'm trying to write a Unix script that will let me print the Python help page for a given module. 我正在尝试编写一个Unix脚本,让我打印给定模块的Python帮助页面。 My code so far is below:
我的代码到目前为止如下:
#!/usr/bin/env python
if __name__ == "__main__":
import sys
if sys.argv[1].endswith(".py"):
__import__(sys.argv[1][:-3])
help(sys.argv[1][:-3])
else:
__import__(sys.argv[1])
help(sys.argv[1])
It works almost as I want-- Invoking it on a module pulls up the help page in a buffer similar to invoking less
. 它几乎可以按照我的需要工作 - 在一个模块上调用它会在一个类似于调用
less
的缓冲区中提取帮助页面。 (In other words, help
works exactly as it does when using it in the interpreter.) (换句话说,
help
工作方式与在解释器中使用时完全一样。)
What I would like is to get rid of the buffer and print straight to the stdout so that I can use the command with other Unix commands. 我想要的是摆脱缓冲区并直接打印到stdout,以便我可以使用该命令与其他Unix命令。 I'm wondering if there is a way to do this, and if so, how?
我想知道是否有办法做到这一点,如果有的话,怎么样?
This works for me: 这对我有用:
import pydoc
import math # test
print(pydoc.render_doc(math))
Try this? 试试这个?
import pydoc
pydoc.help(xrange)
Or if you want it in string format 或者如果你想要它的字符串格式
pydoc.getdoc(xrange)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.