Print docstrings to shell

Discussion in 'Python' started by Gnarlodious, Feb 2, 2011.

  1. Gnarlodious

    Gnarlodious Guest

    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.

    -- Gnarlie
    Gnarlodious, Feb 2, 2011
    #1
    1. Advertising

  2. Gnarlodious

    Carl Banks Guest

    On Feb 1, 4:11 pm, Gnarlodious <> 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
    Carl Banks, Feb 2, 2011
    #2
    1. Advertising

  3. Gnarlodious

    Gnarlodious Guest

    Thank you for the help, I learned a few things. The André solution
    renders the colors but needs q-q to quit. The Carl solution 1 prints
    colors and requires q to quit. The Carl solution 2 prints colorlessly,
    it looks good for exporting to a file. Everything I need.

    -- Gnarlie
    http://Gnarlodious.com
    Gnarlodious, Feb 2, 2011
    #3
  4. On Tue, 01 Feb 2011 16:11:51 -0800, Gnarlodious wrote:

    > Can I run a script in bash and print out its docstrings to the bash
    > shell? I tried this at the end:

    [...]
    > 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.


    Use the Source, Luke :)

    See what help() does, and modify it appropriately. See the site and pydoc
    modules for more information.

    But there's no need to reinvent the wheel. pydoc already does what you
    want (or at least what I think you want). Just run "pydoc name" from
    bash. So long as name is a module in the python path (in other words, so
    long as "import name" will work), then pydoc will see it too. If you want
    to write the output out to a file, use "pydoc -w name".


    --
    Steven
    Steven D'Aprano, Feb 2, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,653
    Jonathan Bromley
    Feb 18, 2005
  2. keto
    Replies:
    0
    Views:
    897
  3. Christian Heimes
    Replies:
    0
    Views:
    584
    Christian Heimes
    Feb 27, 2008
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    335
    David Cournapeau
    Dec 30, 2008
  5. Salty Dog
    Replies:
    4
    Views:
    782
    Salty Dog
    Feb 28, 2005
Loading...

Share This Page