1
2
3
4
5
6
7
|
<span id="pydoc-documentation-generator-and-online-help-system"></span><h1>pydoc — Documentation generator and online help system</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/pydoc.py">Lib/pydoc.py</a></p> <p>The <a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code>pydoc</code></a> module automatically generates documentation from Python modules. The documentation can be presented as pages of text on the console, served to a web browser, or saved to HTML files.</p> <p>For modules, classes, functions and methods, the displayed documentation is derived from the docstring (i.e. the <code>__doc__</code> attribute) of the object, and recursively of its documentable members. If there is no docstring, <a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code>pydoc</code></a> tries to obtain a description from the block of comment lines just above the definition of the class, function or method in the source file, or at the top of the module (see <a class="reference internal" href="inspect#inspect.getcomments" title="inspect.getcomments"><code>inspect.getcomments()</code></a>).</p> <p>The built-in function <a class="reference internal" href="functions#help" title="help"><code>help()</code></a> invokes the online help system in the interactive interpreter, which uses <a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code>pydoc</code></a> to generate its documentation as text on the console. The same text documentation can also be viewed from outside the Python interpreter by running <strong class="program">pydoc</strong> as a script at the operating system’s command prompt. For example, running</p> <pre data-language="python">python -m pydoc sys
</pre> <p>at a shell prompt will display documentation on the <a class="reference internal" href="sys#module-sys" title="sys: Access system-specific parameters and functions."><code>sys</code></a> module, in a style similar to the manual pages shown by the Unix <strong class="program">man</strong> command. The argument to <strong class="program">pydoc</strong> can be the name of a function, module, or package, or a dotted reference to a class, method, or function within a module or module in a package. If the argument to <strong class="program">pydoc</strong> looks like a path (that is, it contains the path separator for your operating system, such as a slash in Unix), and refers to an existing Python source file, then documentation is produced for that file.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>In order to find objects and their documentation, <a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code>pydoc</code></a> imports the module(s) to be documented. Therefore, any code on module level will be executed on that occasion. Use an <code>if __name__ == '__main__':</code> guard to only execute code when a file is invoked as a script and not just imported.</p> </div> <p>When printing output to the console, <strong class="program">pydoc</strong> attempts to paginate the output for easier reading. If the <span class="target" id="index-1"></span><code>PAGER</code> environment variable is set, <strong class="program">pydoc</strong> will use its value as a pagination program.</p> <p>Specifying a <code>-w</code> flag before the argument will cause HTML documentation to be written out to a file in the current directory, instead of displaying text on the console.</p> <p>Specifying a <code>-k</code> flag before the argument will search the synopsis lines of all available modules for the keyword given as the argument, again in a manner similar to the Unix <strong class="program">man</strong> command. The synopsis line of a module is the first line of its documentation string.</p> <p>You can also use <strong class="program">pydoc</strong> to start an HTTP server on the local machine that will serve documentation to visiting web browsers. <strong class="program">python -m pydoc -p 1234</strong> will start a HTTP server on port 1234, allowing you to browse the documentation at <code>http://localhost:1234/</code> in your preferred web browser. Specifying <code>0</code> as the port number will select an arbitrary unused port.</p> <p><strong class="program">python -m pydoc -n <hostname></strong> will start the server listening at the given hostname. By default the hostname is ‘localhost’ but if you want the server to be reached from other machines, you may want to change the host name that the server responds to. During development this is especially useful if you want to run pydoc from within a container.</p> <p><strong class="program">python -m pydoc -b</strong> will start the server and additionally open a web browser to a module index page. Each served page has a navigation bar at the top where you can <em>Get</em> help on an individual item, <em>Search</em> all modules with a keyword in their synopsis line, and go to the <em>Module index</em>, <em>Topics</em> and <em>Keywords</em> pages.</p> <p>When <strong class="program">pydoc</strong> generates documentation, it uses the current environment and path to locate modules. Thus, invoking <strong class="program">pydoc spam</strong> documents precisely the version of the module you would get if you started the Python interpreter and typed <code>import spam</code>.</p> <p>Module docs for core modules are assumed to reside in <code>https://docs.python.org/X.Y/library/</code> where <code>X</code> and <code>Y</code> are the major and minor version numbers of the Python interpreter. This can be overridden by setting the <span class="target" id="index-2"></span><code>PYTHONDOCS</code> environment variable to a different URL or to a local directory containing the Library Reference Manual pages.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <code>-b</code> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>The <code>-g</code> command line option was removed.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span><a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code>pydoc</code></a> now uses <a class="reference internal" href="inspect#inspect.signature" title="inspect.signature"><code>inspect.signature()</code></a> rather than <a class="reference internal" href="inspect#inspect.getfullargspec" title="inspect.getfullargspec"><code>inspect.getfullargspec()</code></a> to extract signature information from callables.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <code>-n</code> option.</p> </div> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/pydoc.html" class="_attribution-link">https://docs.python.org/3.12/library/pydoc.html</a>
</p>
</div>
|