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

 更新时间:2015年06月02日 17:04:53   投稿:junjie  
这篇文章主要介绍了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 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' 

执行结果:


您可能感兴趣的文章:

相关文章

  • python3使用sqlite3构建本地持久化缓存的过程

    python3使用sqlite3构建本地持久化缓存的过程

    日常python开发中会遇到数据持久化的问题,今天记录下如何使用sqlite3进行数据持久化,并提供示例代码及数据查看工具,需要的朋友可以参考下
    2023-11-11
  • 简单介绍Python中的decode()方法的使用

    简单介绍Python中的decode()方法的使用

    这篇文章主要介绍了简单介绍Python中的decode()方法的使用,是Python入门学习当中必须掌握的基础知识,需要的朋友可以参考下
    2015-05-05
  • 利用Python实现网络运维自动化的实战案例

    利用Python实现网络运维自动化的实战案例

    Python作为一种简洁而强大的编程语言,已经成为网络运维自动化的热门选择,本文将介绍如何利用Python实现网络设备配置管理、监控和故障排除等自动化任务,并提供代码示例,需要的朋友可以参考下
    2024-03-03
  • Python 字符串换行的多种方式

    Python 字符串换行的多种方式

    本文通过四种方法给大家介绍了Python 字符串换行的方式,在文中最下面通过代码给大家介绍了python代码过长的换行方法,需要的朋友可以参考下
    2018-09-09
  • python常用的各种排序算法原理与实现方法小结

    python常用的各种排序算法原理与实现方法小结

    这篇文章主要介绍了python常用的各种排序算法原理与实现方法,结合实例形式总结分析了冒泡排序、插入排序、选择排序、快速排序等排序算法的相关原理与实现方法,需要的朋友可以参考下
    2023-04-04
  • python 绘制国旗的示例

    python 绘制国旗的示例

    这篇文章主要介绍了python 绘制国旗的示例,帮助大家利用python绘制,处理图像,感兴趣的朋友可以了解下
    2020-09-09
  • 一篇文章带你了解python标准库--sys模块

    一篇文章带你了解python标准库--sys模块

    这篇文章主要介绍了Python标准库之Sys模块使用详解,本文讲解了使用sys模块获得脚本的参数、处理模块、使用sys模块操作模块搜索路径、使用sys模块查找内建模块、使用sys模块查找已导入的模块等使用案例,需要的朋友可以参考下
    2021-08-08
  • 利用Python解决构造回文字符串问题的方法

    利用Python解决构造回文字符串问题的方法

    回文字符串是指正读和反读都相同的字符串,例如"aba"或"abba",构造回文字符串问题通常涉及从给定字符串中删除某些字符,以形成最长的回文子序列,或者计算形成回文所需的最小删除次数,本文将详细介绍如何使用Python和动态规划算法来解决构造回文字符串问题
    2025-04-04
  • 基于Python编写一个点名器的示例代码

    基于Python编写一个点名器的示例代码

    想起小学的时候老师想点名找小伙伴回答问题的时候,老师竟斥巨资买了个点名器。今日无聊便敲了敲小时候老师斥巨资买的点名器,希望对大家有帮助
    2022-07-07
  • 在 Python 中接管键盘中断信号的实现方法

    在 Python 中接管键盘中断信号的实现方法

    要使用信号,我们需用导入 Python 的signal库。然后自定义一个信号回调函数,当 Python 收到某个信号时,调用这个函数。 ,下面通过实例代码给大家介绍在 Python 中接管键盘中断信号,需要的朋友可以参考下
    2020-02-02

最新评论