Python 標準庫 pydoc 能從模組的 docstring 自動生成文檔,支援終端機文字、HTML 檔案、本地 HTTP 伺服器三種輸出模式,零配置、即拿即用,是中小專案文檔化的首選工具。
| 平台 | 指令 | 範例 |
|---|---|---|
| Windows | python -m pydoc <module> | python -m pydoc math |
| Linux/Unix | pydoc <module> | pydoc os |
pydoc -h # 顯示說明
pydoc -k keyword # 全模組關鍵字搜尋
pydoc -p 1234 # 啟動 HTTP 伺服器於 localhost:1234
pydoc -w module # 生成 module.html 於當前目錄
D:\> python -m pydoc -p 1234
pydoc server ready at https://localhost:1234/

瀏覽器即可檢索所有已安裝模組文檔,支援搜尋與超連結導航。
$ pydoc -k xml.sax
xml.sax (package) - Simple API for XML (SAX) implementation for Python.
xml.sax.handler - core classes of SAX version 2.0
...
D:\LearnPython> python -m pydoc -w math math.html
wrote math.html

右上角顯示 (built-in) 表示內建模組。
# my_doc.py
'''
Showoff features of Pydoc module
This is easy module to demonstrate docstrings
'''
__authors__ = 'Alice & Fred'
__version__ = 'version 1.10'
class MyClass:
'''Demonstrate Class Docstrings'''
def __init__(self, spam=1, eggs=2):
'''Set default attribute values
spam - a processed meat product
eggs - a fine breakfast for lumberjacks'''
self.spam = spam
self.eggs = eggs
def square(x):
'''Square of the param x'''
return x * x
D:\LearnPython> python -m pydoc my_doc
# 終端機顯示結構化文檔
D:\LearnPython> python -m pydoc -w my_doc my_doc.html
wrote my_doc.html
