Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Ka-Ping Yee <ping@lfw.org> |
| 8 | .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
| 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | **Source code:** :source:`Lib/pydoc.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | .. index:: |
| 13 | single: documentation; generation |
| 14 | single: documentation; online |
| 15 | single: help; online |
| 16 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 17 | -------------- |
| 18 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | The :mod:`pydoc` module automatically generates documentation from Python |
| 20 | modules. The documentation can be presented as pages of text on the console, |
| 21 | served to a Web browser, or saved to HTML files. |
| 22 | |
Georg Brandl | 8ed75cd | 2014-10-31 10:25:48 +0100 | [diff] [blame] | 23 | For modules, classes, functions and methods, the displayed documentation is |
| 24 | derived from the docstring (i.e. the :attr:`__doc__` attribute) of the object, |
| 25 | and 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 |
| 27 | above the definition of the class, function or method in the source file, or at |
| 28 | the top of the module (see :func:`inspect.getcomments`). |
| 29 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | The built-in function :func:`help` invokes the online help system in the |
| 31 | interactive interpreter, which uses :mod:`pydoc` to generate its documentation |
| 32 | as text on the console. The same text documentation can also be viewed from |
| 33 | outside the Python interpreter by running :program:`pydoc` as a script at the |
| 34 | operating system's command prompt. For example, running :: |
| 35 | |
| 36 | pydoc sys |
| 37 | |
| 38 | at a shell prompt will display documentation on the :mod:`sys` module, in a |
| 39 | style similar to the manual pages shown by the Unix :program:`man` command. The |
| 40 | argument to :program:`pydoc` can be the name of a function, module, or package, |
| 41 | or a dotted reference to a class, method, or function within a module or module |
| 42 | in a package. If the argument to :program:`pydoc` looks like a path (that is, |
| 43 | it contains the path separator for your operating system, such as a slash in |
| 44 | Unix), and refers to an existing Python source file, then documentation is |
| 45 | produced for that file. |
| 46 | |
Benjamin Peterson | da10d3b | 2009-01-01 00:23:30 +0000 | [diff] [blame] | 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 | |
Zachary Ware | 4847035 | 2014-11-26 23:44:25 -0600 | [diff] [blame] | 54 | When printing output to the console, :program:`pydoc` attempts to paginate the |
| 55 | output for easier reading. If the :envvar:`PAGER` environment variable is set, |
| 56 | :program:`pydoc` will use its value as a pagination program. |
| 57 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 58 | Specifying a ``-w`` flag before the argument will cause HTML documentation |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | to be written out to a file in the current directory, instead of displaying text |
| 60 | on the console. |
| 61 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 62 | Specifying a ``-k`` flag before the argument will search the synopsis |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | lines of all available modules for the keyword given as the argument, again in a |
| 64 | manner similar to the Unix :program:`man` command. The synopsis line of a |
| 65 | module is the first line of its documentation string. |
| 66 | |
| 67 | You can also use :program:`pydoc` to start an HTTP server on the local machine |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 68 | that will serve documentation to visiting Web browsers. :program:`pydoc -p 1234` |
| 69 | will start a HTTP server on port 1234, allowing you to browse the |
| 70 | documentation at ``http://localhost:1234/`` in your preferred Web browser. |
| 71 | Specifying ``0`` as the port number will select an arbitrary unused port. |
| 72 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 73 | :program:`pydoc -b` will start the server and additionally open a web |
| 74 | browser to a module index page. Each served page has a navigation bar at the |
| 75 | top where you can *Get* help on an individual item, *Search* all modules with a |
| 76 | keyword in their synopsis line, and go to the *Module index*, *Topics* and |
| 77 | *Keywords* pages. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | When :program:`pydoc` generates documentation, it uses the current environment |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 80 | and path to locate modules. Thus, invoking :program:`pydoc spam` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | documents precisely the version of the module you would get if you started the |
| 82 | Python interpreter and typed ``import spam``. |
| 83 | |
| 84 | Module docs for core modules are assumed to reside in |
Georg Brandl | e73778c | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 85 | ``https://docs.python.org/X.Y/library/`` where ``X`` and ``Y`` are the |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 86 | major and minor version numbers of the Python interpreter. This can |
| 87 | be overridden by setting the :envvar:`PYTHONDOCS` environment variable |
| 88 | to a different URL or to a local directory containing the Library |
| 89 | Reference Manual pages. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 91 | .. versionchanged:: 3.2 |
Zachary Ware | 61aebca | 2014-11-26 22:57:35 -0600 | [diff] [blame] | 92 | Added the ``-b`` option. |
| 93 | |
| 94 | .. versionchanged:: 3.3 |
| 95 | The ``-g`` command line option was removed. |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 96 | |
| 97 | .. versionchanged:: 3.4 |
| 98 | :mod:`pydoc` now uses :func:`inspect.signature` rather than |
| 99 | :func:`inspect.getfullargspec` to extract signature information from |
| 100 | callables. |