Introduction

If you are a developer who writes code in Python, you might have heard of PyDoc. It is a tool that automatically generates documentation for your code based on the docstrings you write. (I am using the Thonny code editor for the video tutorial at the end of this page). In this post, I will show you several Python PyDoc examples and how you can use this tool effectively.

why use pyDoc?

Why would you use documentation at all? Why is documenting your code good? Here are some reasons:

  • It helps you and others understand your code better. By writing docstrings, you are forced to think about what your code does and how it works. This can help you spot errors, improve your design, and refactor your code. It also helps other developers who need to read, use, or modify your code. They can quickly get an overview of your code’s purpose, functionality, and parameters without having to read every line of code.
  • It saves you time and effort. By using PyDoc, you don’t have to write separate documentation files for your code. You can just write docstrings as you code and let PyDoc do the rest. This way, you can keep your documentation up to date with your code and avoid inconsistencies or errors. You also don’t have to worry about formatting or styling your documentation, as PyDoc takes care of that for you.
  • It makes your code more professional and reusable. By documenting your code with PyDoc, you are following a standard and widely used convention in the Python community. This makes your code more readable, consistent, and compatible with other tools and libraries. It also makes your code more attractive and useful for other developers who might want to use or contribute to your code.

As you can see, PyDoc is a powerful and convenient tool that can help you document your Python code easily and effectively. If you want to learn more about PyDoc, you can check out the official documentation here: https://docs.python.org/3/library/pydoc.html

Now let’s get to the coding part!

How to install pyDoc?

The following command in the terminal can give you the features of PyDoc:

python -m pydoc
pydoc for documentation

how to read modules documentations?

Now, as an example, using the following command in the terminal gives you the documentation on random module in python:

python -m pydoc random

how to use docstrings in pyDoc?

Docstrings are special comments that describe what your functions, classes, modules, and variables do. They are enclosed in triple quotes, like this:

def add(x, y):
""" Returns the sum of x and y. """
    return x + y

PyDoc can read these docstrings and create HTML pages that show the structure and description of your code. You can also use PyDoc to launch a web server that lets you browse the documentation online, or to create text files that you can print or share.

how to use pyDoc to document python code?

To use PyDoc inside your code editor, you need to import it in your Python script and call the relevant functions. For example, to generate HTML documentation for a module named my_module.py, you can write:

import pydoc

def add(x, y):
"""
    Params:
      x and b are numbers (int or float)
    Returns: 
      the sum of x and y.
"""
    return x + y

pydoc.writedoc('my_module')

This will create a file named my_module.html in the current directory.

pydoc for html documentation

Alternatively, you can run Pydoc as a command-line tool. For example, to create the html documentation for the the same module in a terminal, you can type:

python -m pydoc -w my_module

To start a web server that serves the documentation for all installed modules on your local machine, you can type:

python -m pydoc -p 1234
pydoc for module documentation

In the above, you can choose another port number as well. Then you can open your browser and go to http://localhost:1234 to browse the documentation.

difference between pyDoc and Sphinx

Pydoc is a useful tool for developers who want to quickly access the documentation of Python modules without leaving their code editor or terminal. It can also help create consistent and comprehensive documentation for your own modules.

Pydoc and Sphinx are both tools that can generate documentation for Python modules, but they have some differences. Pydoc is a built-in module that comes with the standard Python distribution. It can produce simple and plain documentation in HTML or text format, or display it in a terminal or a web server. Pydoc uses the docstrings of the modules to generate the documentation, so it only documents what the developer has written in the code.

Sphinx is a third-party tool that can be installed separately. It can produce more complex and rich documentation in various formats, such as HTML, PDF, ePub, or LaTeX. Sphinx uses reStructuredText as the markup language for the documentation, which allows more flexibility and customization. Sphinx can also integrate with other tools, such as autodoc, doctest, intersphinx, or napoleon, to enhance the documentation process.

Pydoc and Sphinx are both useful tools for Python developers, but they have different purposes and features. Pydoc is more suitable for quick and simple documentation of modules, while Sphinx is more suitable for creating comprehensive and professional documentation of projects.

Don’t forget to leave a like to the video or a comment. 🙂

Similar Posts