简体   繁体   English

在终端中阅读 python 文档?

[英]Reading python documentation in the terminal?

Is there a way to install the python documentation that would make it available as if it was a manpage?有没有办法安装 python 文档,使它像联机帮助页一样可用? (I know you can download the sourcefiles for the documentation and read them in vim, using less or whatever but I was thinking about something a bit less manual. Don't want to roll my own.) (我知道您可以下载文档的源文件并在 vim 中阅读它们,使用更少或其他任何东西,但我正在考虑一些更少手动的东西。不想自己动手。)

It's not an exact copy of the documentation, but there's the builtin help() function .它不是文档的精确副本,但有内置的help()函数

In an interactive python session, you just call help(whatever_you_want_to_read_about) , for example:在交互式 python 会话中,您只需调用help(whatever_you_want_to_read_about) ,例如:

>>> help(all)
Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.

Alternatively, you can start an interactive help session like this:或者,您可以像这样启动交互式帮助会话:

C:\Users\Rawing>python -c "help()"

Welcome to Python 3.4!  This is the interactive help utility.

help>

And then just type the function/class/module you want to know about:然后只需键入您想了解的函数/类/模块:

help> all
Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.

On Debian (and derived distributions, like Ubuntu) install pydoc package.在 Debian(和衍生发行版,如 Ubuntu)上安装pydoc包。 Then you can use pydoc whatever command.然后你可以使用pydoc whatever命令。

I don't if this is what you wanted but you everything you can do in IDLE you can do on the command line.如果这是您想要的,我不知道,但是您可以在 IDLE 中做的所有事情都可以在命令行上做。 Example:例子:

C:>python
>>help(print())
>>help(plt.plot())

This way you can access documentation这样您就可以访问文档

I don't know if that's exactly what you are looking for, but the python interactive console offers a help command.我不知道这是否正是您正在寻找的,但 python 交互式控制台提供了一个help命令。 You can use it in the following manner.您可以通过以下方式使用它。

>>> help()

Welcome to Python 3.6's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.6/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> list

This will output the whole documentation for all of the list methods.这将输出所有list方法的整个文档。

You can use help(Class-name/method-name/anything) .您可以使用help(Class-name/method-name/anything) But also using __doc__但也使用__doc__

A special __doc__ docstring is attached to every class and method.每个类和方法都附加了一个特殊的__doc__文档字符串 For example look what i typed into my interpreter.例如,看看我在解释器中输入了什么。

>>> print(str.__doc__)
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
>>> print(int.__doc__)
int(x=0) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4

It even works for modules.它甚至适用于模块。

>>> import math
>>> math.__doc__
'This module is always available.  It provides access to the\nmathematical functions defined by the C standard.'
>>> math.ceil.__doc__
'ceil(x)\n\nReturn the ceiling of x as an Integral.\nThis is the smallest integer >= x.'
>>> 

Since every class has a __doc__ which is a docstring attached to it you can call it using the class_name.__doc__由于每个类都有一个__doc__ ,它是一个附加的文档字符串,您可以使用class_name.__doc__调用它

>>> print(ord.__doc__)
Return the Unicode code point for a one-character string.

You can use the BIF help()您可以使用BIF help()

In terminal go to Python REPL like在终端中转到 Python REPL

python

Now type help()现在输入帮助()

Now for example if you wish to get help for a class say IPv4Network which is in ipaddress package then you just have to specify the fully qualified path for IPv4Network ie ipaddress.IPv4Network现在,例如,如果您希望获得ipaddress 包中的 IPv4Network类的帮助,那么您只需指定 IPv4Network 的完全限定路径,即ipaddress.IPv4Network

EXAMPLE:例子:

$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help()
Welcome to Python 3.6's help utility!
help> ipaddress.IPv4Network
Help on class IPv4Network in ipaddress:

ipaddress.IPv4Network = class IPv4Network(_BaseV4, _BaseNetwork)
 |  This class represents and manipulates 32-bit IPv4 network + addresses..
 |  
 |  Attributes: [examples for IPv4Network('192.0.2.0/27')]
 |      .network_address: IPv4Address('192.0.2.0')
 |      .hostmask: IPv4Address('0.0.0.31')
 |      .broadcast_address: IPv4Address('192.0.2.32')
 |      .netmask: IPv4Address('255.255.255.224')
 |      .prefixlen: 27
 |  
 |  Method resolution order:
 |      IPv4Network
 |      _BaseV4
 |      _BaseNetwork
 |      _IPAddressBase
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, address, strict=True)
 |      Instantiate a new IPv4 network object.
 |      
 |      Args:
 |          address: A string or integer representing the IP [& network].
 |            '192.0.2.0/24'
 |            '192.0.2.0/255.255.255.0'
 |            '192.0.0.2/0.0.0.255'
 |            are all functionally the same in IPv4. Similarly,
 |            '192.0.2.1'

and so on等等

I will answer since I don't satisfied with the accepted answer.我会回答,因为我对接受的答案不满意。 Probably because I don't use IDLE.可能是因为我不使用 IDLE。

Note that, I use Ubuntu (terminal).请注意,我使用的是 Ubuntu(终端)。 Probably in other OSs, it works the same.可能在其他操作系统中,它的工作原理相同。

Is there a way to install that?有没有办法安装它? No need.没必要。 I found that it comes by default.我发现它是默认出现的。 How to access that?如何访问它? Use help() command in python' shell.在 python 的 shell 中使用help()命令。

  1. In the shell, type command help() .在 shell 中,键入命令help()
  2. Now that you're in the help utility, enter anything that you want to read its documentation.现在您在帮助实用程序中,输入您想要阅读其文档的任何内容。 Press q to quit the documentation and type quit to quit the help utility.q退出文档并键入quit退出帮助实用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM