Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :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 Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 17 | **Source code:** :source:`Lib/pydoc.py` |
| 18 | |
| 19 | -------------- |
| 20 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 21 | The :mod:`pydoc` module automatically generates documentation from Python |
| 22 | modules. The documentation can be presented as pages of text on the console, |
| 23 | served to a Web browser, or saved to HTML files. |
| 24 | |
Georg Brandl | 5003801 | 2014-10-31 10:25:48 +0100 | [diff] [blame] | 25 | For modules, classes, functions and methods, the displayed documentation is |
| 26 | derived from the docstring (i.e. the :attr:`__doc__` attribute) of the object, |
| 27 | and recursively of its documentable members. If there is no docstring, |
| 28 | :mod:`pydoc` tries to obtain a description from the block of comment lines just |
| 29 | above the definition of the class, function or method in the source file, or at |
| 30 | the top of the module (see :func:`inspect.getcomments`). |
| 31 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | The built-in function :func:`help` invokes the online help system in the |
| 33 | interactive interpreter, which uses :mod:`pydoc` to generate its documentation |
| 34 | as text on the console. The same text documentation can also be viewed from |
| 35 | outside the Python interpreter by running :program:`pydoc` as a script at the |
| 36 | operating system's command prompt. For example, running :: |
| 37 | |
| 38 | pydoc sys |
| 39 | |
| 40 | at a shell prompt will display documentation on the :mod:`sys` module, in a |
| 41 | style similar to the manual pages shown by the Unix :program:`man` command. The |
| 42 | argument to :program:`pydoc` can be the name of a function, module, or package, |
| 43 | or a dotted reference to a class, method, or function within a module or module |
| 44 | in a package. If the argument to :program:`pydoc` looks like a path (that is, |
| 45 | it contains the path separator for your operating system, such as a slash in |
| 46 | Unix), and refers to an existing Python source file, then documentation is |
| 47 | produced for that file. |
| 48 | |
Georg Brandl | 1ffbfbc | 2008-12-27 19:11:15 +0000 | [diff] [blame] | 49 | .. note:: |
| 50 | |
| 51 | In order to find objects and their documentation, :mod:`pydoc` imports the |
| 52 | module(s) to be documented. Therefore, any code on module level will be |
| 53 | executed on that occasion. Use an ``if __name__ == '__main__':`` guard to |
| 54 | only execute code when a file is invoked as a script and not just imported. |
| 55 | |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 56 | Specifying a ``-w`` flag before the argument will cause HTML documentation |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | to be written out to a file in the current directory, instead of displaying text |
| 58 | on the console. |
| 59 | |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 60 | Specifying a ``-k`` flag before the argument will search the synopsis |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 61 | lines of all available modules for the keyword given as the argument, again in a |
| 62 | manner similar to the Unix :program:`man` command. The synopsis line of a |
| 63 | module is the first line of its documentation string. |
| 64 | |
| 65 | You can also use :program:`pydoc` to start an HTTP server on the local machine |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 66 | that will serve documentation to visiting Web browsers. :program:`pydoc -p 1234` |
| 67 | will start a HTTP server on port 1234, allowing you to browse |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 68 | the documentation at ``http://localhost:1234/`` in your preferred Web browser. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 69 | :program:`pydoc -g` will start the server and additionally bring up a |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 70 | small :mod:`Tkinter`\ -based graphical interface to help you search for |
| 71 | documentation pages. |
| 72 | |
| 73 | When :program:`pydoc` generates documentation, it uses the current environment |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 74 | and path to locate modules. Thus, invoking :program:`pydoc spam` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 75 | documents precisely the version of the module you would get if you started the |
| 76 | Python interpreter and typed ``import spam``. |
| 77 | |
| 78 | Module docs for core modules are assumed to reside in |
Georg Brandl | 06f3b3b | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 79 | https://docs.python.org/library/. This can be overridden by setting the |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 80 | :envvar:`PYTHONDOCS` environment variable to a different URL or to a local |
| 81 | directory containing the Library Reference Manual pages. |
| 82 | |