Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print module documentation in Python

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.

like image 237
Phil Avatar asked Oct 24 '12 17:10

Phil


People also ask

How do I print a Python module document?

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.

How do you show documents in Python?

Python has a built-in help() function that can access this information and prints the results. For example, to see the documentation of the built-in len function, you can do the following: In [1]: help(len) Help on built-in function len in module builtins: len(...)

What is print (__ doc __)?

The print(__doc__) command simply re-uses that documentation string to write it to your terminal each time you run the script, and any other python tool (like the interactive interpreter help() function, for example) can introspect that same value.

How do I view a Python module?

We can also use the inspect module in python to locate a module. We will use inspect. getfile() method of inspect module to get the path. This method will take the module's name as an argument and will return its path.


1 Answers

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 
like image 124
Ashwini Chaudhary Avatar answered Sep 28 '22 10:09

Ashwini Chaudhary