在使用python的过程中有时需要import其他的包模块,而此时我们需要查看这个模块中提供了哪些函数,是否有像linux man一样可以查询的功能?在python中我们可以通过进入python控制台并导入相关的包模块,再使用help(模块名)来查看这个包模块的信息及相关的函数接口说明等。

以下已pycurl包举例说明用法:

1. 进入python 并导入包

root@ubuntu:/home/lvjc/myPython# python
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pycurl
>>>

2.通过help命令查看你需要man的函数

>>> help(pycurl.Curl.getinfo)
 Help on method_descriptor:

getinfo(...)
    getinfo(info) -> Result
    
    Extract and return information from a curl session.
    
    Corresponds to `curl_easy_getinfo`_ in libcurl, where *option* is
    the same as the ``CURLINFO_*`` constants in libcurl, except that the
    ``CURLINFO_`` prefix has been removed. (See below for exceptions.)
    *Result* contains an integer, float or string, depending on which
    option is given. The ``getinfo`` method should not be called unless
    ``perform`` has been called and finished.
    
    In order to distinguish between similarly-named CURLOPT and CURLINFO
    constants, some have ``OPT_`` and ``INFO_`` prefixes. These are
    ``INFO_FILETIME``, ``OPT_FILETIME``, ``INFO_COOKIELIST`` (but ``setopt`` uses
    ``COOKIELIST``!), ``INFO_CERTINFO``, and ``OPT_CERTINFO``.
    
    The value returned by ``getinfo(INFO_CERTINFO)`` is a list with one element
    per certificate in the chain, starting with the leaf; each element is a
    sequence of *(key, value)* tuples.
    
    Example usage::
    
        import pycurl
        c = pycurl.Curl()
        c.setopt(pycurl.URL, "http://sf.net")
        c.setopt(pycurl.FOLLOWLOCATION, 1)
        c.perform()
        print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL)
        ...
        --> 200 "http://sourceforge.net/"
    
    
    Raises pycurl.error exception upon failure.
    
    .. _curl_easy_getinfo:
        http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
(END)

3. pydoc的用法:

pydoc模块可以从python源码文件中获取docstring,然后生成帮助信息。

3.1 纯文本帮助信息

如在控制台输入命令 pydoc string 则会在控制台生成string的纯文本帮助信息。

root@ubuntu:/home/lvjc/myPython# pydoc string
Help on module string:

NAME
    string - A collection of string operations (most are no longer used).

FILE
    /usr/lib/python2.7/string.py

MODULE DOCS
    http://docs.python.org/library/string

DESCRIPTION
    Warning: most of the code you see here isn't normally used nowadays.
    Beginning with Python 1.6, many of these functions are implemented as
    methods on the standard string object. They used to be implemented by
    a built-in module called strop, but strop is now obsolete itself.
    
    Public module variables:
    
    whitespace -- a string containing all characters considered whitespace
    lowercase -- a string containing all characters considered lowercase letters
    uppercase -- a string containing all characters considered uppercase letters
    letters -- a string containing all characters considered letters
    digits -- a string containing all characters considered decimal digits
    hexdigits -- a string containing all characters considered hexadecimal digits
:

3.2 HTML帮助信息

pydoc还可以生成HTML格式的帮助信息,可以将HTML帮助信息生成到静态文本中,也可以启动一个Web服务器在线浏览帮助文档。

root@ubuntu:/home/lvjc/myPython# pydoc -w string //在当前目录创建string.html文件
root@ubuntu:/home/lvjc/myPython# pydoc -p 5000  //启动一个web服务器监听 http://localhost:5000/