Python文檔生成工具pydoc使用介紹
在Python中有很多很好的工具來(lái)生成字符串文檔(docstring),比如說(shuō): epydoc、doxygen、sphinx,但始終覺(jué)得pydoc還是不錯(cuò)的工具,用法非常簡(jiǎn)單,功能也算不錯(cuò),本文主要介紹pydoc.
pydoc是Python自帶的模塊,主要用于從python模塊中自動(dòng)生成文檔,這些文檔可以基于文本呈現(xiàn)的、也可以生成WEB 頁(yè)面的,還可以在服務(wù)器上以瀏覽器的方式呈現(xiàn)!
【用法】
Windows下:
D:\>python -m pydoc <modulename> # 比如說(shuō): python -m pydoc math
-m參數(shù):Python以腳本的方法運(yùn)行模塊
Linux/Unix下:
$ pydoc <modulename> # 比如說(shuō): pydoc
【幫助】
$ pydoc -h
pydoc - the Python documentation tool
pydoc <name> ...
Show text documentation on something. <name> may be the name of a
Python keyword, topic, function, module, or package, or a dotted
reference to a class or function within a module or module in a
package. If <name> contains a '/', it is used as the path to a
Python source file to document. If name is 'keywords', 'topics',
or 'modules', a listing of these things is displayed.
pydoc -k <keyword>
Search for a keyword in the synopsis lines of all available modules.
pydoc -p <port>
Start an HTTP server on the given port on the local machine.
pydoc -w <name> ...
Write out the HTML documentation for a module to a file in the current
directory. If <name> contains a '/', it is treated as a filename; if
it names a directory, documentation is written for all the contents.
【參數(shù) -p】在本地機(jī)器上,按照給定的端口啟動(dòng)HTTP,
D:\>python -m pydoc -p 1234 #比如說(shuō): 端口為1234
pydoc server ready at http://localhost:1234/
pydoc server stopped
在IE中輸入:http://localhost:1234/,效果如圖:

【參數(shù) -k】在所有可用的模塊中按關(guān)鍵字搜索
$ pydoc -k xml.sax
xml.sax (package) - Simple API for XML (SAX) implementation for Python.
xml.sax._exceptions - Different kinds of SAX Exceptions
xml.sax.expatreader - SAX driver for the pyexpat C module. This driver works with
xml.sax.handler - This module contains the core classes of version 2.0 of SAX for Python.
xml.sax.saxutils - A library of useful helper classes to the SAX classes, for the
xml.sax.xmlreader - An XML Reader is the SAX 2 name for an XML parser. XML Parsers
【參數(shù) -w】將指定模塊的文本字符串生成HTML格式
比如說(shuō),在Window下面,執(zhí)行下面命令:
D:\Learn\Python>python -m pydoc math -w math.html # math是模塊名,-w:寫
那么在D:\Learn\Python目錄下會(huì)生成math.html文件,顯示如下:

因?yàn)槭亲詭У哪K,所以右上角顯示(built-in)字樣
【例子】自寫的模塊my_doc.py
'''''
Showoff features of Pydoc module
This is easy module to demonstrate docstrings
'''
__authors__ = 'Alice & Fred'
__version__ = 'version 1.10'
__license__ = 'Copyright...'
class MyClass:
'''''
Demonstrate Class Docstrings
'''
def __init__(self, spam=1, eggs=2):
'''''
Set the default attributevalues only
Keyword arguments:
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
執(zhí)行命令:
D:\Learn\Python> python -m pydoc my_doc
執(zhí)行結(jié)果:
Help on module my_doc:
NAME
my_doc
FILE
d:\learn\python\my_doc.py
DESCRIPTION
Showoff features of Pydoc module
This is easy module to demonstrate docstrings
CLASSES
MyClass
class MyClass
| Demonstrate Class Docstrings
|
| Methods defined here:
|
| __init__(self, spam=1, eggs=2)
| Set the default attributevalues only
| Keyword arguments:
| spam - a processed meat product
| eggs - a fine breakfast for lumberjacks
FUNCTIONS
square(x)
Square of the param <x>
DATA
__authors__ = 'Alice & Fred'
__license__ = 'Copyright...'
__version__ = 'version 1.10'
VERSION
version 1.10
執(zhí)行命令:
d:\Learn\Python>python -m pydoc -w my_doc my_doc.html
wrote my_doc.html
no Python documentation found for 'my_doc.html'
執(zhí)行結(jié)果:

相關(guān)文章
python3使用sqlite3構(gòu)建本地持久化緩存的過(guò)程
日常python開(kāi)發(fā)中會(huì)遇到數(shù)據(jù)持久化的問(wèn)題,今天記錄下如何使用sqlite3進(jìn)行數(shù)據(jù)持久化,并提供示例代碼及數(shù)據(jù)查看工具,需要的朋友可以參考下2023-11-11
簡(jiǎn)單介紹Python中的decode()方法的使用
這篇文章主要介紹了簡(jiǎn)單介紹Python中的decode()方法的使用,是Python入門學(xué)習(xí)當(dāng)中必須掌握的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
利用Python實(shí)現(xiàn)網(wǎng)絡(luò)運(yùn)維自動(dòng)化的實(shí)戰(zhàn)案例
Python作為一種簡(jiǎn)潔而強(qiáng)大的編程語(yǔ)言,已經(jīng)成為網(wǎng)絡(luò)運(yùn)維自動(dòng)化的熱門選擇,本文將介紹如何利用Python實(shí)現(xiàn)網(wǎng)絡(luò)設(shè)備配置管理、監(jiān)控和故障排除等自動(dòng)化任務(wù),并提供代碼示例,需要的朋友可以參考下2024-03-03
python常用的各種排序算法原理與實(shí)現(xiàn)方法小結(jié)
這篇文章主要介紹了python常用的各種排序算法原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了冒泡排序、插入排序、選擇排序、快速排序等排序算法的相關(guān)原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2023-04-04
一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--sys模塊
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)之Sys模塊使用詳解,本文講解了使用sys模塊獲得腳本的參數(shù)、處理模塊、使用sys模塊操作模塊搜索路徑、使用sys模塊查找內(nèi)建模塊、使用sys模塊查找已導(dǎo)入的模塊等使用案例,需要的朋友可以參考下2021-08-08
利用Python解決構(gòu)造回文字符串問(wèn)題的方法
回文字符串是指正讀和反讀都相同的字符串,例如"aba"或"abba",構(gòu)造回文字符串問(wèn)題通常涉及從給定字符串中刪除某些字符,以形成最長(zhǎng)的回文子序列,或者計(jì)算形成回文所需的最小刪除次數(shù),本文將詳細(xì)介紹如何使用Python和動(dòng)態(tài)規(guī)劃算法來(lái)解決構(gòu)造回文字符串問(wèn)題2025-04-04
基于Python編寫一個(gè)點(diǎn)名器的示例代碼
想起小學(xué)的時(shí)候老師想點(diǎn)名找小伙伴回答問(wèn)題的時(shí)候,老師竟斥巨資買了個(gè)點(diǎn)名器。今日無(wú)聊便敲了敲小時(shí)候老師斥巨資買的點(diǎn)名器,希望對(duì)大家有幫助2022-07-07
在 Python 中接管鍵盤中斷信號(hào)的實(shí)現(xiàn)方法
要使用信號(hào),我們需用導(dǎo)入 Python 的signal庫(kù)。然后自定義一個(gè)信號(hào)回調(diào)函數(shù),當(dāng) Python 收到某個(gè)信號(hào)時(shí),調(diào)用這個(gè)函數(shù)。 ,下面通過(guò)實(shí)例代碼給大家介紹在 Python 中接管鍵盤中斷信號(hào),需要的朋友可以參考下2020-02-02

