使用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模块

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫竹梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值