How can I print the Python help page directly to stdout?

I am trying to write a Unix script that will allow me to 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"):


It works almost the way I want it to. Calling it on a module brings up the help page in a buffer, similar to calling less

. (In other words, it help

works exactly the same as when used in the interpreter.)

I would like to get rid of the buffer and print directly to stdout so that I can use the command with other Unix commands. I am wondering if there is a way to do this, and if so, how?


source to share

2 answers

This works for me:

import pydoc

import math # test




Try it?

import pydoc


Or if you want it in string format





All Articles