LinuxCommandLibrary

pydoc

Display documentation for Python modules

TLDR

Print documentation on a subject (Python keyword, topic, function, module, package, etc.)

$ pydoc [subject]
copy

Start an HTTP server on an arbitrary unused port and open a [b]rowser to see the documentation
$ pydoc -b
copy

Display help
$ pydoc
copy

SYNOPSIS

pydoc [options] [name]
pydoc -k keyword
pydoc -p port
pydoc -g
pydoc -w name

PARAMETERS

name
    Specifies the name of a module, function, class, method, or topic for which to display documentation. If omitted, pydoc launches an interactive help session.

-k keyword
    Searches the documentation for a specified keyword, listing modules that contain the keyword in their summary lines.

-p port
    Starts an HTTP server on the specified port, allowing you to browse documentation using a web browser. Typically used with -b or -n.

-g
    Launches a graphical user interface (GUI) browser for documentation, requiring the Tkinter module to be installed.

-w name
    Writes the HTML documentation for the specified name to a file in the current directory, typically named 'name.html'.

-n hostname
    Specifies the hostname for the HTTP server when used with -p. This allows for binding the server to a specific network interface.

-b
    Starts an HTTP server (implies -p and -n) and automatically opens a web browser to the server's documentation page. Uses the default web browser configured on your system.

DESCRIPTION

pydoc is a command-line utility and Python module that automatically generates and displays documentation from Python modules, functions, classes, and keywords. It acts as an invaluable tool for developers seeking quick access to reference materials without leaving the terminal. pydoc can present help()-style output directly in your console, launch an HTTP server to browse documentation through a web browser, or even write module documentation to an HTML file for offline viewing. By leveraging Python's introspection capabilities and docstrings, pydoc provides dynamic and up-to-date information on installed Python components. It's a standard part of the Python distribution, offering a convenient way to understand how various Python elements work.

CAVEATS

The -g option requires Python's Tkinter module to be installed, which might not be present in minimal Python installations. The -b option relies on a default web browser being correctly configured and accessible in your environment. pydoc documents installed Python modules; it cannot directly document arbitrary .py files unless they are discoverable via PYTHONPATH or the current working directory. The quality and completeness of documentation depend entirely on the docstrings written by module developers.

ENVIRONMENT VARIABLES

Several environment variables can influence pydoc's behavior:
PAGER: Specifies the program to use for displaying documentation (e.g., less, more). If not set, pydoc attempts to use less or more.
BROWSER: Specifies the web browser to use for the -b option. If not set, pydoc tries to find a suitable browser.
PYTHONDOCS: Specifies the path to the Python library documentation (e.g., .../pythonX.Y/Doc/).
PYTHONPATH: Influences where pydoc searches for modules, similar to how Python itself finds modules.

EXAMPLES OF USAGE

To display documentation for the 'os' module:
pydoc os

To search for modules containing 'datetime':
pydoc -k datetime

To start an HTTP server on port 8000:
pydoc -p 8000

To write HTML documentation for the 'json' module to a file:
pydoc -w json

HISTORY

pydoc has been a standard and integral part of the Python distribution since at least Python 2.2, if not earlier. Its inception aimed to provide developers with a convenient and consistent way to access documentation for Python modules, classes, functions, and topics directly from the command line or via a web interface, mirroring the functionality of the help() built-in function within the Python interpreter. It leverages Python's powerful introspection capabilities to dynamically generate documentation from docstrings, making it a continuously relevant tool as libraries evolve.

SEE ALSO

python(1), man(1), help()

Copied to clipboard