I'm trying to write a Unix script that will let me print the Python help page for a given module. 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
. (In other words, help
works exactly as it does when using it in the interpreter.)
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. 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.