blob: f956b9d2176187524009069259256e64fe425997 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pydoc` --- Documentation generator and online help system
2===============================================================
3
4.. module:: pydoc
5 :synopsis: Documentation generator and online help system.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
8.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/pydoc.py`
Georg Brandl116aa622007-08-15 14:28:22 +000011
Georg Brandl116aa622007-08-15 14:28:22 +000012.. index::
13 single: documentation; generation
14 single: documentation; online
15 single: help; online
16
Raymond Hettinger469271d2011-01-27 20:38:46 +000017--------------
18
Georg Brandl116aa622007-08-15 14:28:22 +000019The :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
Georg Brandl8ed75cd2014-10-31 10:25:48 +010023For modules, classes, functions and methods, the displayed documentation is
24derived from the docstring (i.e. the :attr:`__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
Georg Brandl116aa622007-08-15 14:28:22 +000030The 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 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
Benjamin Petersonda10d3b2009-01-01 00:23:30 +000047.. 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
Zachary Ware48470352014-11-26 23:44:25 -060054When printing output to the console, :program:`pydoc` attempts to paginate the
55output for easier reading. If the :envvar:`PAGER` environment variable is set,
56:program:`pydoc` will use its value as a pagination program.
57
Éric Araujo713d3032010-11-18 16:38:46 +000058Specifying a ``-w`` flag before the argument will cause HTML documentation
Georg Brandl116aa622007-08-15 14:28:22 +000059to be written out to a file in the current directory, instead of displaying text
60on the console.
61
Éric Araujo713d3032010-11-18 16:38:46 +000062Specifying a ``-k`` flag before the argument will search the synopsis
Georg Brandl116aa622007-08-15 14:28:22 +000063lines of all available modules for the keyword given as the argument, again in a
64manner similar to the Unix :program:`man` command. The synopsis line of a
65module is the first line of its documentation string.
66
67You can also use :program:`pydoc` to start an HTTP server on the local machine
Nick Coghlan7bb30b72010-12-03 09:29:11 +000068that will serve documentation to visiting Web browsers. :program:`pydoc -p 1234`
69will start a HTTP server on port 1234, allowing you to browse the
70documentation at ``http://localhost:1234/`` in your preferred Web browser.
71Specifying ``0`` as the port number will select an arbitrary unused port.
72
Feanil Patel6a396c92017-09-14 17:54:09 -040073:program:`pydoc -n <hostname>` will start the server listening at the given
74hostname. By default the hostname is 'localhost' but if you want the server to
75be reached from other machines, you may want to change the host name that the
76server responds to. During development this is especially useful if you want
77to run pydoc from within a container.
78
Nick Coghlan7bb30b72010-12-03 09:29:11 +000079:program:`pydoc -b` will start the server and additionally open a web
80browser to a module index page. Each served page has a navigation bar at the
81top where you can *Get* help on an individual item, *Search* all modules with a
82keyword in their synopsis line, and go to the *Module index*, *Topics* and
83*Keywords* pages.
Georg Brandl116aa622007-08-15 14:28:22 +000084
85When :program:`pydoc` generates documentation, it uses the current environment
Éric Araujo713d3032010-11-18 16:38:46 +000086and path to locate modules. Thus, invoking :program:`pydoc spam`
Georg Brandl116aa622007-08-15 14:28:22 +000087documents precisely the version of the module you would get if you started the
88Python interpreter and typed ``import spam``.
89
90Module docs for core modules are assumed to reside in
Georg Brandle73778c2014-10-29 08:36:35 +010091``https://docs.python.org/X.Y/library/`` where ``X`` and ``Y`` are the
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000092major and minor version numbers of the Python interpreter. This can
93be overridden by setting the :envvar:`PYTHONDOCS` environment variable
94to a different URL or to a local directory containing the Library
95Reference Manual pages.
Georg Brandl116aa622007-08-15 14:28:22 +000096
Nick Coghlan7bb30b72010-12-03 09:29:11 +000097.. versionchanged:: 3.2
Zachary Ware61aebca2014-11-26 22:57:35 -060098 Added the ``-b`` option.
99
100.. versionchanged:: 3.3
101 The ``-g`` command line option was removed.
Larry Hastings3732ed22014-03-15 21:13:56 -0700102
103.. versionchanged:: 3.4
104 :mod:`pydoc` now uses :func:`inspect.signature` rather than
105 :func:`inspect.getfullargspec` to extract signature information from
106 callables.
Feanil Patel6a396c92017-09-14 17:54:09 -0400107
108.. versionchanged:: 3.7
109 Added the ``-n`` option.