Learn How to View Python Documentation Using Pydoc

Learn How to View Python Documentation Using Pydoc

  • Python
  • 4 mins read

In this tutorial, you'll learn how to view Python documentation using the pydoc module.

In Python, the pydoc module is a document generator. You can view the documentation on the console, in the browser, and save as HTML pages using the pydoc. Below are the examples:

Generate Documentation for Python Using Pydoc

First, check the pydoc module options. Open the command prompt in Windows or terminal in Linux and give the following commands:

python -m pydoc

Output

pydoc - the Python documentation tool

pydoc <name> ...
    Show text documentation on something.  <name> may be the name of a
    Python keyword, topic, function, module, or package, or a dotted
    reference to a class or function within a module or module in a
    package.  If <name> contains a '\', it is used as the path to a
    Python source file to document. If name is 'keywords', 'topics',
    or 'modules', a listing of these things is displayed.

pydoc -k <keyword>
    Search for a keyword in the synopsis lines of all available modules.

pydoc -n <hostname>
    Start an HTTP server with the given hostname (default: localhost).

pydoc -p <port>
    Start an HTTP server on the given port on the local machine.  Port
    number 0 can be used to get an arbitrary unused port.

pydoc -b
    Start an HTTP server on an arbitrary unused port and open a Web browser
    to interactively browse documentation.  This option can be used in
    combination with -n and/or -p.

pydoc -w <name> ...
    Write out the HTML documentation for a module to a file in the current
    directory.  If <name> contains a '\', it is treated as a filename; if
    it names a directory, documentation is written for all the contents.

Now we will check the above options for pydoc one by one.

pydoc <name>

View the documentation for any Python command. Below is an example to view the help doc for if condition:

python -m pydoc if

Output

The "if" statement
******************

The "if" statement is used for conditional execution:

   if_stmt ::= "if" expression ":" suite
               ( "elif" expression ":" suite )*
               ["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.

Related help topics: TRUTHVALUE

pydoc -k <keyword>

To find a keyword in Python documentation use -k option. In the following example, it will search the documentation for keyword XML:

python -m pydoc -k XML

Output (Displaying few lines)

test.test_docxmlrpc
test.test_xml_dom_minicompat
test.test_xml_etree
test.test_xml_etree_c
test.test_xmlrpc
test.test_xmlrpc_net
test.xmltests
xml - Core XML support for Python.
xml.dom - W3C Document Object Model implementation for Python.
xml.dom.NodeFilter
xml.dom.domreg - Registration facilities for DOM. This module should not be used
xml.dom.expatbuilder - Facility to use the Expat parser to load a minidom instance
xml.dom.minicompat - Python version compatibility support for minidom.
....

pydoc -p <port>

Python will create an HTTP server for the given port number for the documentation. For example, if you will specify the port 321, then you can view the Python documentation on URL http://localhost:321. Below is an example:

python -m pydoc -p 321

Output

Server ready at http://localhost:321/
Server commands: [b]rowser, [q]uit
server>

When you open the above URL in the browser, you will get the output as shown in the featured image of this article. At the server prompt, press b to open the browser and press q to exit.

pydoc -b

When you use pydoc with -b option, it will create and start the server with the port available on the local machine. Below is an example:

python -m pydoc -b

Output

Server ready at http://localhost:60806/
Server commands: [b]rowser, [q]uit
server>

It will open the browser window automatically using the above URL.

pydoc -w <name>

Pydoc with -w <name> option, create out an HTML documentation for a module to a file in the current directory. Below is an example:

F:\md python_docs
F:\cd python_docs
F:\python_docs\python -m pydoc -w xml
wrote xml

You will find the document xml in the f:\python_docs directory and the content of that file would be as shown in the below image:

Document written through pydoc.

See also: