blob: 14dc58b50dffd2c30251c54d59313345b311e92c [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.
6.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
7.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
8
9
Georg Brandl116aa622007-08-15 14:28:22 +000010.. index::
11 single: documentation; generation
12 single: documentation; online
13 single: help; online
14
15The :mod:`pydoc` module automatically generates documentation from Python
16modules. The documentation can be presented as pages of text on the console,
17served to a Web browser, or saved to HTML files.
18
19The built-in function :func:`help` invokes the online help system in the
20interactive interpreter, which uses :mod:`pydoc` to generate its documentation
21as text on the console. The same text documentation can also be viewed from
22outside the Python interpreter by running :program:`pydoc` as a script at the
23operating system's command prompt. For example, running ::
24
25 pydoc sys
26
27at a shell prompt will display documentation on the :mod:`sys` module, in a
28style similar to the manual pages shown by the Unix :program:`man` command. The
29argument to :program:`pydoc` can be the name of a function, module, or package,
30or a dotted reference to a class, method, or function within a module or module
31in a package. If the argument to :program:`pydoc` looks like a path (that is,
32it contains the path separator for your operating system, such as a slash in
33Unix), and refers to an existing Python source file, then documentation is
34produced for that file.
35
Benjamin Petersonda10d3b2009-01-01 00:23:30 +000036.. note::
37
38 In order to find objects and their documentation, :mod:`pydoc` imports the
39 module(s) to be documented. Therefore, any code on module level will be
40 executed on that occasion. Use an ``if __name__ == '__main__':`` guard to
41 only execute code when a file is invoked as a script and not just imported.
42
Éric Araujo713d3032010-11-18 16:38:46 +000043Specifying a ``-w`` flag before the argument will cause HTML documentation
Georg Brandl116aa622007-08-15 14:28:22 +000044to be written out to a file in the current directory, instead of displaying text
45on the console.
46
Éric Araujo713d3032010-11-18 16:38:46 +000047Specifying a ``-k`` flag before the argument will search the synopsis
Georg Brandl116aa622007-08-15 14:28:22 +000048lines of all available modules for the keyword given as the argument, again in a
49manner similar to the Unix :program:`man` command. The synopsis line of a
50module is the first line of its documentation string.
51
52You can also use :program:`pydoc` to start an HTTP server on the local machine
Nick Coghlan7bb30b72010-12-03 09:29:11 +000053that will serve documentation to visiting Web browsers. :program:`pydoc -p 1234`
54will start a HTTP server on port 1234, allowing you to browse the
55documentation at ``http://localhost:1234/`` in your preferred Web browser.
56Specifying ``0`` as the port number will select an arbitrary unused port.
57
Éric Araujo713d3032010-11-18 16:38:46 +000058:program:`pydoc -g` will start the server and additionally bring up a
Ezio Melotti1a263ad2010-03-14 09:51:37 +000059small :mod:`tkinter`\ -based graphical interface to help you search for
Nick Coghlan7bb30b72010-12-03 09:29:11 +000060documentation pages. The ``-g`` option is deprecated, since the server can
61now be controlled directly from HTTP clients.
62
63:program:`pydoc -b` will start the server and additionally open a web
64browser to a module index page. Each served page has a navigation bar at the
65top where you can *Get* help on an individual item, *Search* all modules with a
66keyword in their synopsis line, and go to the *Module index*, *Topics* and
67*Keywords* pages.
Georg Brandl116aa622007-08-15 14:28:22 +000068
69When :program:`pydoc` generates documentation, it uses the current environment
Éric Araujo713d3032010-11-18 16:38:46 +000070and path to locate modules. Thus, invoking :program:`pydoc spam`
Georg Brandl116aa622007-08-15 14:28:22 +000071documents precisely the version of the module you would get if you started the
72Python interpreter and typed ``import spam``.
73
74Module docs for core modules are assumed to reside in
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000075``http://docs.python.org/X.Y/library/`` where ``X`` and ``Y`` are the
76major and minor version numbers of the Python interpreter. This can
77be overridden by setting the :envvar:`PYTHONDOCS` environment variable
78to a different URL or to a local directory containing the Library
79Reference Manual pages.
Georg Brandl116aa622007-08-15 14:28:22 +000080
Nick Coghlan7bb30b72010-12-03 09:29:11 +000081.. versionchanged:: 3.2
Georg Brandl34e9fc22010-12-03 09:45:33 +000082 Added the ``-b`` option, deprecated the ``-g`` option.