Python⽂档⽣成⼯具pydoc使⽤介绍
在Python中有很多很好的⼯具来⽣成字符串⽂档(docstring),⽐如说: epydoc、doxygen、sphinx,但始终觉得pydoc还是不错的⼯具,⽤法⾮常简单,功能也算不错,本⽂主要介绍pydoc.
pydoc是Python⾃带的模块,主要⽤于从python模块中⾃动⽣成⽂档,这些⽂档可以基于⽂本呈现的、也可以⽣成WEB 页⾯的,还可以在服务器上以浏览器的⽅式呈现!
【⽤法】
Windows下:
复制代码代码如下:
D:\>python -m pydoc <modulename>  # ⽐如说: python -m pydoc math
-m参数:Python以脚本的⽅法运⾏模块
Linux/Unix下:
复制代码代码如下:
$ pydoc <modulename>              # ⽐如说: 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.
【参数 -p】在本地机器上,按照给定的端⼝启动HTTP,
复制代码代码如下:
D:\>python -m pydoc -p 1234 #⽐如说: 端⼝为1234
pydoc server ready at localhost:1234/
pydoc server stopped
【参数 -k】在所有可⽤的模块中按关键字搜索
复制代码代码如下:
$ pydoc -k xml.sax
xml.sax (package) - Simple API for XML (SAX) implementation for Python.
xml.sax._exceptions - Different kinds of SAX Exceptions
patreader - 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
lreader - An XML Reader is the SAX 2 name for an XML parser. XML Parsers
【参数 -w】将指定模块的⽂本字符串⽣成HTML格式
⽐如说,在Window下⾯,执⾏下⾯命令:
复制代码代码如下:
D:\Learn\Python>python -m pydoc math -w math.html  # math是模块名,-w:写
那么在D:\Learn\Python⽬录下会⽣成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'
__license__  = ''
class MyClass:
'''''python中文文档
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
def square(x):
'''''
Square of the param <x>
'''
return x * x
执⾏命令:
复制代码代码如下:
D:\Learn\Python> python -m pydoc my_doc
执⾏结果:
复制代码代码如下:
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__ = ''
__version__ = 'version 1.10'
VERSION
version 1.10
执⾏命令:
复制代码代码如下:
d:\Learn\Python>python -m pydoc -w my_doc my_doc.html  wrote my_doc.html
no Python documentation found for 'my_doc.html'
执⾏结果: