使用Pydoc生成文档

Python中本身带有很多实用的工具,如pydoc。pydoc模块主要用来从Python模块中提取信息并生成文档。

使用方法

在Windows和Linux下的使用方法有些区别。

Windows

python -m pydoc <modulename>

如:

C:\>python -m pydoc module_test 

NB:module_test是自定义的模块,不要添加文件后缀。

Linux

pydoc <modulename>

如:

$ pydoc module_test

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.

$ 

无参数 <name>

显示文档,<name>可以是相查询的任何东西。如关键字、函数、模块、包等等。

C:\>python -m pydoc print
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

C:\>python -m pydoc random
Help on module random:

NAME
    random - Random variable generators.

DESCRIPTION
        integers
        --------
               uniform within range

        sequences
        ---------
               pick random element
               pick random sample
               generate random permutation

        distributions on the real line:
        ------------------------------
               uniform
               triangular
               normal (Gaussian)
               lognormal
               negative exponential
               gamma
...

参数-k <keyword>

在可用模块中按关键字搜索。

C:\>python -m pydoc -k print
calendar - Calendar printing functions
email.quoprimime - Quoted-printable content transfer encoding per RFCs 2045-2047
.
encodings.quopri_codec - Codec for quoted-printable encoding.
json.tool - Command-line tool to validate and pretty-print JSON
lib2to3.fixes.fix_print - Fixer for print.
pprint - Support to pretty-print lists, tuples, & dictionaries recursively.
pstats - Class for printing reports on profiled python code.
quopri - Conversions to/from quoted-printable transport encoding as per RFC 1521
.
test.test_pprint
test.test_print
traceback - Extract, format and print information about Python stack traces.

参数-p <port>

在当前主机启用HTTP服务,使用指定的端口。

C:\>python -m pydoc -p 53241
Server ready at http://localhost:53241/
Server commands: [b]rowser, [q]uit
server> b
server>

如:

pydoc

这时可以点击来跟踪链接进行查看相应的文档。

参数-w <name>

生成HTML文档。

首先编写一个模块,此外为class_test.py

"""
A simple test.
"""

if __name__ == "__main__":
    print("hello")

然后执行如下:

C:\>python -m pydoc -w class_test
wrote class_test.html

此时在C盘根目录下生成了class_test.html文件,就可以在浏览器中进行查看了。

更多请参考Python的pydoc模块

posted @ 2017-10-22 15:51  枫竹梦  阅读(1157)  评论(0编辑  收藏  举报