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的类变量和成员变量用法实例教程
Aug 25 Python
Python中处理unchecked未捕获异常实例
Jan 17 Python
利用Python中的输入和输出功能进行读取和写入的教程
Apr 14 Python
Python中type的构造函数参数含义说明
Jun 21 Python
Python中的urllib模块使用详解
Jul 07 Python
Windows下搭建python开发环境详细步骤
Jul 20 Python
python的exec、eval使用分析
Dec 11 Python
python+matplotlib演示电偶极子实例代码
Jan 12 Python
python模块之paramiko实例代码
Jan 31 Python
Pandas分组与排序的实现
Jul 23 Python
python实现对列表中的元素进行倒序打印
Nov 23 Python
图文详解matlab原始处理图像几何变换
Jul 09 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
4月1日重磅发布!《星际争霸II》6.0.0版本更新
2020/04/09 星际争霸
PHP版国家代码、缩写查询函数代码
2011/08/14 PHP
php短址转换实现方法
2015/02/25 PHP
js类 from qq
2006/11/13 Javascript
jQuery插件实现表格隔行换色且感应鼠标高亮行变色
2013/09/22 Javascript
JS模态窗口返回值兼容问题的完美解决方法
2016/05/28 Javascript
AngularJS入门之动画
2016/07/27 Javascript
js中数组插入、删除元素操作的方法
2017/02/15 Javascript
数组Array的排序sort方法
2017/02/17 Javascript
原生js实现简单的模态框示例
2017/09/08 Javascript
vue 获取url参数、get参数返回数组的操作
2020/11/12 Javascript
python实现simhash算法实例
2014/04/25 Python
Python实例分享:快速查找出被挂马的文件
2014/06/08 Python
全面解读Python Web开发框架Django
2014/06/30 Python
在Python中使用列表生成式的教程
2015/04/27 Python
Python实现脚本锁功能(同时只能执行一个脚本)
2017/05/10 Python
Python之str操作方法(详解)
2017/06/19 Python
基于Python log 的正确打开方式
2018/04/28 Python
python发送邮件脚本
2018/05/22 Python
python生成lmdb格式的文件实例
2018/11/08 Python
在python中利用opencv简单做图片比对的方法
2019/01/24 Python
Django视图扩展类知识点详解
2019/10/25 Python
python中rc1什么意思
2020/06/19 Python
萨克斯第五大道的折扣店:Saks Fifth Avenue OFF 5TH
2016/08/25 全球购物
什么是Rollback Segment
2013/04/22 面试题
ktv中秋节活动方案
2014/01/30 职场文书
公司办公室岗位职责
2014/03/19 职场文书
股东合作协议书范本
2014/04/14 职场文书
企业员工薪酬方案
2014/06/04 职场文书
基层党员学习党的群众路线教育实践活动心得体会
2014/11/04 职场文书
国家助学金感谢信
2015/01/21 职场文书
2015年全国助残日活动方案
2015/05/04 职场文书
村官2015年度工作总结
2015/10/14 职场文书
安装配置mysql及Navicat prenium的详细流程
2021/06/10 MySQL
NASA 机智号火星直升机拍到了毅力号设备碎片
2022/04/29 数码科技
Golang入门之计时器
2022/05/04 Golang