Print docstrings to shell

Carl Banks pavlovevidence at gmail.com
Tue Feb 1 21:53:09 EST 2011


On Feb 1, 4:11 pm, Gnarlodious <gnarlodi... at gmail.com> wrote:
> Can I run a script in bash and print out its docstrings to the bash
> shell? I tried this at the end:
>
> print(help(__file__))
>
> Runnig the script:
> python ~/Sites/Sectrum/Harmonics.py
>
> but all it spit out was:
>
> no Python documentation found for '~/Sites/Sectrum/Harmonics.py'
>
> However in the interactive shell it prints out the class structure
> nicely. What I really want to see is this output in the bash window.

The help() function prints the documentation itself itself (piping it
to a pager if possible).  It doesn't return the help text.

If that's what you want, then probably the most foolproof way is:

help(sys.modules[__name__])

This'll work whether it's a module or script.  If you just want to
print the documentation and bypass the pager, then I think something
like this will do it:

import pydoc
print pydoc.render_doc(sys.modules[__name__])


Carl Banks



More information about the Python-list mailing list