Python > Python Ecosystem and Community > Community Resources > Official Python Documentation
Accessing the Official Python Documentation Using `pydoc`
This snippet demonstrates how to access the official Python documentation directly from your terminal using the pydoc
module. pydoc
generates HTML pages of documentation for Python modules right from the source code. It can also be used to run a documentation server.
Basic Usage of `pydoc`
This code snippet shows how to import the When you uncomment pydoc
module. To use it, uncomment either pydoc.help('math')
to print the documentation to your console or pydoc.serve(port=8000)
to start a local documentation server.pydoc.help('math')
, running this code will display the documentation for the math
module in your terminal. When you uncomment pydoc.serve(port=8000)
and run the script, it will start a web server on port 8000 of your local machine, allowing you to browse all available Python documentation in your web browser.
import pydoc
# Get help on the 'math' module
# pydoc.help('math')
# Serve documentation on localhost, port 8000 (open in your browser at http://localhost:8000)
# pydoc.serve(port=8000)
Running `pydoc` from the Command Line
This approach is generally more convenient than using pydoc
is most commonly used directly from the command line. The first command shows how to display the documentation for the math
module directly in your terminal. The second command shows how to start a local documentation server with a browser window opened automatically.pydoc
within a Python script, as it requires no code and can be quickly executed from any terminal.
# To get help on a module (e.g., 'math') from the command line:
# python -m pydoc math
# To start a documentation server on the command line:
# python -m pydoc -b # The -b option opens a browser window
Concepts Behind the Snippet
pydoc
leverages the docstrings present within Python modules, classes, and functions. Docstrings are multiline strings enclosed in triple quotes ('''
or """
) and are used to document the purpose, usage, and parameters of code elements. pydoc
parses these docstrings and formats them for easy readability, either in the terminal or through a web browser.
Real-Life Use Case
Imagine you're working on a complex data analysis project and need to understand the usage of a specific function within the pandas
library. Instead of searching online, you can quickly access the official documentation using pydoc pandas.DataFrame.groupby
in your terminal. This provides immediate access to the function's description, parameters, and examples, streamlining your workflow.
Best Practices
Always start by checking the official documentation using pydoc
before resorting to external search engines. This ensures you're working with the most accurate and up-to-date information. Use the command line interface for quick lookups, and the server mode for browsing extensive documentation.
When to Use It
Use pydoc
when you need quick access to the official Python documentation, especially when you're offline or want to avoid the distractions of online search. It's also useful for exploring the structure and contents of unfamiliar modules.
FAQ
-
Why is `pydoc` useful?
pydoc
is useful because it provides direct access to the official Python documentation, allowing developers to quickly understand the usage and purpose of modules, classes, and functions without needing an internet connection. -
How do I use `pydoc` in a script?
Import thepydoc
module and use thepydoc.help()
function to display documentation in your terminal, orpydoc.serve()
to start a local documentation server. -
How do I access documentation for a specific function in a module?
Using the command line, runpython -m pydoc module.function
. For example, to view documentation for thesqrt
function in themath
module, runpython -m pydoc math.sqrt
.