Python文档生成工具pydoc使用介绍

yipeiwu_com5年前Python基础

在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 http://localhost:1234/
pydoc server stopped

在IE中输入:http://localhost:1234/,效果如图:

【参数 -k】在所有可用的模块中按关键字搜索

复制代码 代码如下:

$ 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 

【参数 -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__  = '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 

执行命令:

复制代码 代码如下:

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__ = 'Copyright...' 
    __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' 

执行结果:


相关文章

Django之编辑时根据条件跳转回原页面的方法

在要跳转的编辑页面: #首先获取当期的url: curr_url = self.request.GET.urlencode() #创建一个QueryDict对象: params =...

Python循环实现n的全排列功能

描述: 输入一个大于0的整数n,输出1到n的全排列: 例如: n=3,输出[[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2]...

初步认识Python中的列表与位运算符

初步认识Python中的列表与位运算符

Python列表 List(列表) 是 Python 中使用最频繁的数据类型。 列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。 列表用[...

Django3.0 异步通信初体验(小结)

Django3.0 异步通信初体验(小结)

此前博主曾经写过一篇博文,介绍了Django3.0的新特性,其中最主要的就是加入对ASGI的支持,实现全双工的异步通信。 2019年12月2日,Django终于正式发布了3.0版本。怀着...

Python + OpenCV 实现LBP特征提取的示例代码

Python + OpenCV 实现LBP特征提取的示例代码

背景 看了些许的纹理特征提取的paper,想自己实现其中部分算法,看看特征提取之后的效果是怎样 运行环境 Mac OS Python3.0 Anaconda3(集成了很多包...