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


Posted in Python onJune 02, 2015

在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/,效果如图:

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

【参数 -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文件,显示如下:

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

因为是自带的模块,所以右上角显示(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' 

执行结果:

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

Python 相关文章推荐
Python使用tablib生成excel文件的简单实现方法
Mar 16 Python
python如何派生内置不可变类型并修改实例化行为
Mar 21 Python
python实现数独游戏 java简单实现数独游戏
Mar 30 Python
python调用matlab的m自定义函数方法
Feb 18 Python
python二进制文件的转译详解
Jul 03 Python
Django用户认证系统 User对象解析
Aug 02 Python
python同步两个文件夹下的内容
Aug 29 Python
使用Python实现牛顿法求极值
Feb 10 Python
Python图像处理库PIL的ImageFilter模块使用介绍
Feb 26 Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 Python
C3 线性化算法与 MRO之Python中的多继承
Oct 05 Python
Python基本的内置数据类型及使用方法
Apr 13 Python
自己使用总结Python程序代码片段
Jun 02 #Python
python执行子进程实现进程间通信的方法
Jun 02 #Python
Python多进程并发(multiprocessing)用法实例详解
Jun 02 #Python
使用pdb模块调试Python程序实例
Jun 02 #Python
python使用xmlrpclib模块实现对百度google的ping功能
Jun 02 #Python
python基于xmlrpc实现二进制文件传输的方法
Jun 02 #Python
Python中的自省(反射)详解
Jun 02 #Python
You might like
解析php5配置使用pdo
2013/07/03 PHP
关于PHP开发的9条建议
2015/07/27 PHP
php微信支付接口开发程序
2016/08/02 PHP
JavaScript 语言基础知识点总结(思维导图)
2013/11/10 Javascript
javascript避免数字计算精度误差的方法详解
2014/03/05 Javascript
JavaScript实现弹出DIV层同时页面背景渐变成半透明效果
2016/03/25 Javascript
JavaScript判断数组重复内容的两种方法(推荐)
2016/06/06 Javascript
学习Angularjs分页指令
2016/07/01 Javascript
mui上拉加载功能实例详解
2017/04/13 Javascript
vue2 前后端分离项目ajax跨域session问题解决方法
2017/04/27 Javascript
利用node.js如何创建子进程详解
2017/12/09 Javascript
vue非父子组件通信问题及解决方法
2018/06/11 Javascript
nuxt.js中间件实现拦截权限判断的方法
2018/11/21 Javascript
JavaScript制作3D旋转相册
2020/08/02 Javascript
使用grappelli为django admin后台添加模板
2014/11/18 Python
Python的string模块中的Template类字符串模板用法
2016/06/27 Python
详解Python import方法引入模块的实例
2017/08/02 Python
Python文件的读写和异常代码示例
2017/10/31 Python
python 设置文件编码格式的实现方法
2017/12/21 Python
Python 串口读写的实现方法
2019/06/12 Python
python变量命名的7条建议
2019/07/04 Python
pytorch打印网络结构的实例
2019/08/19 Python
python处理自动化任务之同时批量修改word里面的内容的方法
2019/08/23 Python
python3实现网页版raspberry pi(树莓派)小车控制
2020/02/12 Python
使用HTML5 IndexDB存储图像和文件的示例
2018/11/05 HTML / CSS
英国领先的奢侈品零售商之一:CRUISE
2016/12/02 全球购物
约瑟夫·特纳男装:Joseph Turner
2017/10/10 全球购物
我的珠宝盒:Ma boîte à bijoux
2019/08/27 全球购物
写clone()方法时,通常都有一行代码,是什么?
2012/10/31 面试题
平面设计求职信
2014/03/10 职场文书
小学作文评语大全
2014/04/21 职场文书
社区活动总结报告
2014/05/05 职场文书
解放思想演讲稿
2014/09/11 职场文书
2014乡镇干部对照检查材料思想汇报
2014/09/26 职场文书
python turtle绘制多边形和跳跃和改变速度特效
2022/03/16 Python
spring 项目实现限流方法示例
2022/07/15 Java/Android