• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!pydoc` --- Documentation generator and online help system
2================================================================
3
4.. module:: pydoc
5   :synopsis: Documentation generator and online help system.
6
7.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
8.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
9
10**Source code:** :source:`Lib/pydoc.py`
11
12.. index::
13   single: documentation; generation
14   single: documentation; online
15   single: help; online
16
17--------------
18
19The :mod:`!pydoc` module automatically generates documentation from Python
20modules.  The documentation can be presented as pages of text on the console,
21served to a web browser, or saved to HTML files.
22
23For modules, classes, functions and methods, the displayed documentation is
24derived from the docstring (i.e. the :attr:`~definition.__doc__` attribute) of the object,
25and recursively of its documentable members.  If there is no docstring,
26:mod:`!pydoc` tries to obtain a description from the block of comment lines just
27above the definition of the class, function or method in the source file, or at
28the top of the module (see :func:`inspect.getcomments`).
29
30The built-in function :func:`help` invokes the online help system in the
31interactive interpreter, which uses :mod:`!pydoc` to generate its documentation
32as text on the console.  The same text documentation can also be viewed from
33outside the Python interpreter by running :program:`pydoc` as a script at the
34operating system's command prompt. For example, running ::
35
36   python -m pydoc sys
37
38at a shell prompt will display documentation on the :mod:`sys` module, in a
39style similar to the manual pages shown by the Unix :program:`man` command.  The
40argument to :program:`pydoc` can be the name of a function, module, or package,
41or a dotted reference to a class, method, or function within a module or module
42in a package.  If the argument to :program:`pydoc` looks like a path (that is,
43it contains the path separator for your operating system, such as a slash in
44Unix), and refers to an existing Python source file, then documentation is
45produced for that file.
46
47.. note::
48
49   In order to find objects and their documentation, :mod:`!pydoc` imports the
50   module(s) to be documented.  Therefore, any code on module level will be
51   executed on that occasion.  Use an ``if __name__ == '__main__':`` guard to
52   only execute code when a file is invoked as a script and not just imported.
53
54When printing output to the console, :program:`pydoc` attempts to paginate the
55output for easier reading.  If either the :envvar:`MANPAGER` or the
56:envvar:`PAGER` environment variable is set, :program:`pydoc` will use its
57value as a pagination program. When both are set, :envvar:`MANPAGER` is used.
58
59Specifying a ``-w`` flag before the argument will cause HTML documentation
60to be written out to a file in the current directory, instead of displaying text
61on the console.
62
63Specifying a ``-k`` flag before the argument will search the synopsis
64lines of all available modules for the keyword given as the argument, again in a
65manner similar to the Unix :program:`man` command.  The synopsis line of a
66module is the first line of its documentation string.
67
68You can also use :program:`pydoc` to start an HTTP server on the local machine
69that will serve documentation to visiting web browsers.  :program:`python -m pydoc -p 1234`
70will start a HTTP server on port 1234, allowing you to browse the
71documentation at ``http://localhost:1234/`` in your preferred web browser.
72Specifying ``0`` as the port number will select an arbitrary unused port.
73
74:program:`python -m pydoc -n <hostname>` will start the server listening at the given
75hostname.  By default the hostname is 'localhost' but if you want the server to
76be reached from other machines, you may want to change the host name that the
77server responds to.  During development this is especially useful if you want
78to run pydoc from within a container.
79
80:program:`python -m pydoc -b` will start the server and additionally open a web
81browser to a module index page.  Each served page has a navigation bar at the
82top where you can *Get* help on an individual item, *Search* all modules with a
83keyword in their synopsis line, and go to the *Module index*, *Topics* and
84*Keywords* pages.
85
86When :program:`pydoc` generates documentation, it uses the current environment
87and path to locate modules.  Thus, invoking :program:`pydoc spam`
88documents precisely the version of the module you would get if you started the
89Python interpreter and typed ``import spam``.
90
91Module docs for core modules are assumed to reside in
92``https://docs.python.org/X.Y/library/`` where ``X`` and ``Y`` are the
93major and minor version numbers of the Python interpreter.  This can
94be overridden by setting the :envvar:`!PYTHONDOCS` environment variable
95to a different URL or to a local directory containing the Library
96Reference Manual pages.
97
98.. versionchanged:: 3.2
99   Added the ``-b`` option.
100
101.. versionchanged:: 3.3
102   The ``-g`` command line option was removed.
103
104.. versionchanged:: 3.4
105   :mod:`!pydoc` now uses :func:`inspect.signature` rather than
106   :func:`inspect.getfullargspec` to extract signature information from
107   callables.
108
109.. versionchanged:: 3.7
110   Added the ``-n`` option.
111