blob: 8343452b95ae1ad52c55e31bb8af618b13f630be [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
10.. versionadded:: 2.1
11
12.. index::
13 single: documentation; generation
14 single: documentation; online
15 single: help; online
16
Éric Araujo29a0b572011-08-19 02:14:03 +020017**Source code:** :source:`Lib/pydoc.py`
18
19--------------
20
Georg Brandl8ec7f652007-08-15 14:28:01 +000021The :mod:`pydoc` module automatically generates documentation from Python
22modules. The documentation can be presented as pages of text on the console,
23served to a Web browser, or saved to HTML files.
24
25The built-in function :func:`help` invokes the online help system in the
26interactive interpreter, which uses :mod:`pydoc` to generate its documentation
27as text on the console. The same text documentation can also be viewed from
28outside the Python interpreter by running :program:`pydoc` as a script at the
29operating system's command prompt. For example, running ::
30
31 pydoc sys
32
33at a shell prompt will display documentation on the :mod:`sys` module, in a
34style similar to the manual pages shown by the Unix :program:`man` command. The
35argument to :program:`pydoc` can be the name of a function, module, or package,
36or a dotted reference to a class, method, or function within a module or module
37in a package. If the argument to :program:`pydoc` looks like a path (that is,
38it contains the path separator for your operating system, such as a slash in
39Unix), and refers to an existing Python source file, then documentation is
40produced for that file.
41
Georg Brandl1ffbfbc2008-12-27 19:11:15 +000042.. note::
43
44 In order to find objects and their documentation, :mod:`pydoc` imports the
45 module(s) to be documented. Therefore, any code on module level will be
46 executed on that occasion. Use an ``if __name__ == '__main__':`` guard to
47 only execute code when a file is invoked as a script and not just imported.
48
Éric Araujoa8132ec2010-12-16 03:53:53 +000049Specifying a ``-w`` flag before the argument will cause HTML documentation
Georg Brandl8ec7f652007-08-15 14:28:01 +000050to be written out to a file in the current directory, instead of displaying text
51on the console.
52
Éric Araujoa8132ec2010-12-16 03:53:53 +000053Specifying a ``-k`` flag before the argument will search the synopsis
Georg Brandl8ec7f652007-08-15 14:28:01 +000054lines of all available modules for the keyword given as the argument, again in a
55manner similar to the Unix :program:`man` command. The synopsis line of a
56module is the first line of its documentation string.
57
58You can also use :program:`pydoc` to start an HTTP server on the local machine
Éric Araujoa8132ec2010-12-16 03:53:53 +000059that will serve documentation to visiting Web browsers. :program:`pydoc -p 1234`
60will start a HTTP server on port 1234, allowing you to browse
Georg Brandl8ec7f652007-08-15 14:28:01 +000061the documentation at ``http://localhost:1234/`` in your preferred Web browser.
Éric Araujoa8132ec2010-12-16 03:53:53 +000062:program:`pydoc -g` will start the server and additionally bring up a
Georg Brandl8ec7f652007-08-15 14:28:01 +000063small :mod:`Tkinter`\ -based graphical interface to help you search for
64documentation pages.
65
66When :program:`pydoc` generates documentation, it uses the current environment
Éric Araujoa8132ec2010-12-16 03:53:53 +000067and path to locate modules. Thus, invoking :program:`pydoc spam`
Georg Brandl8ec7f652007-08-15 14:28:01 +000068documents precisely the version of the module you would get if you started the
69Python interpreter and typed ``import spam``.
70
71Module docs for core modules are assumed to reside in
Georg Brandl0751d1a2008-01-21 17:13:03 +000072http://docs.python.org/library/. This can be overridden by setting the
Georg Brandl8ec7f652007-08-15 14:28:01 +000073:envvar:`PYTHONDOCS` environment variable to a different URL or to a local
74directory containing the Library Reference Manual pages.
75