45

I know this question is very simple, I know it must have been asked a lot of times and I did my search on both SO and Google but I could not find the answer, probably due to my lack of ability of putting what I seek into a proper sentence.

I want to be able to read the docs of what I import.

For example if I import x by "import x", I want to run this command, and have its docs printed in Python or ipython.

What is this command-function?

Thank you.

PS. I don't mean dir(), I mean the function that will actually print the docs for me to see and read what functionalities etc. this module x has.

3 Answers 3

37

You can use the .__doc__ attribute of the module of function:

In [14]: import itertools

In [15]: print itertools.__doc__
Functional tools for creating and using iterators..........

In [18]: print itertools.permutations.__doc__
permutations(iterable[, r]) --> permutations object

Return successive r-length permutations of elements in the iterable.

permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)

Both of help() and __doc__ work fine on both inbuilt and on our own modules:

file: foo.py

def myfunc():
    """
    this is some info on myfunc

    """
    foo=2
    bar=3


In [4]: help(so27.myfunc)


In [5]: import foo

In [6]: print foo.myfunc.__doc__

     this is some info on func

In [7]: help(foo.myfunc)


Help on function myfunc in module foo:

myfunc()
    this is some info on func
Sign up to request clarification or add additional context in comments.

1 Comment

Ashwini, thanks again and again. Excellent explanation. I have a tiny little question though. Say I'm on ipython client and I run the help command and it prints the docs, how do I go back to ipython? ;-) escape, backspace, space, enter.. no characters take me back except termination of ipython.
24

pydoc foo.bar from the command line or help(foo.bar) or help('foo.bar') from Python.

4 Comments

Thank you Josh! Your answer solves my issue big time as well but for future reference for others I am choosing the other answer as "the" answer since it contains a bit more info for other newbies like myself.
@josh-lee +1 for mentioning the command line option. What if I want to print the documentation of the module from within __main__ function of a module?
I can't thank you enough for this. I was looking for a simple way to have text dump of the module help. This was exactly what I was looking for.
Furthermore, if the doc is too long to read in cmd, we can use pydoc foo.bar > foo.txt to output the doc to foo.txt
2

You can use help() function to display the documentation. or you can choose method.__doc__ descriptor.

Eg: help(input) will give the documentation on input() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.