如何在Linux上查看Python文档?

31

在Windows系统中,Python有一种chm类型的文档,非常方便阅读。 但是在Linux系统中,是否有类似的文档可以供我阅读呢?


你不能只是搜索Python文档网站吗?此外,谷歌在找到正确的文档方面非常出色。 - Some programmer dude
3
给Joachim Pileborg:抱歉,这里的网络一直很差,所以我需要离线文档。 - Tanky Woo
有一天我忘记了如何启动PyDoc本地主机服务器。我开始在Python文档网站上搜索,但没有找到什么有用的信息。所以我转向Stack Exchange,在这里的一个答案给了我我需要的东西。也许我们应该保留这个问题。 - D A Vincent
我想指出并不是所有的Python开发环境都可以实时访问互联网。 - user3481644
9个回答

22

在线文档

最简单的方法是使用谷歌搜索在线文档。并没有一个单一的地方可以找到所有模块的所有文档。不过,以下是一些常见的:

如果您需要离线文档,则还有其他一些可能性:

下载

您可以将文档下载为 HTML 或 PDF:https://docs.python.org/zh-cn/3/download.html

当您运行 web 服务器时,您可以使用 HTML 版本,并像往常一样通过浏览器访问它。HTML 网站看起来就像您习惯的一样。即使是搜索也可以离线工作,因为它是使用 JavaScript 实现的。

enter image description here

PyDoc

某些发行版(如 Debian)提供了一个 python-doc 软件包。您可以通过 pydoc -p [some port number] 或通过 pydoc -g 来访问它。这将创建一个本地 web 服务器。然后您可以打开浏览器查看:

enter image description here

控制台:help(...)

Python 交互式控制台有一个内置的 help(...) 系统。您可以不带参数调用它:

$ python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help()

Welcome to Python 2.7!  This is the online 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/2.7/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, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> 

或者您可以使用一个参数来调用它,这个参数可以是任何东西(模块、类、函数、对象等),它的形式如下:

或者您可以针对想要了解的内容使用一个参数进行调用,该参数可以是任何东西(模块、类、函数、对象等)。其样式如下:

>>> a = {'b':'c'}
>>> help(a)
Help on dict object:

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
 |      in the keyword argument list.  For example:  dict(one=1, two=2)
 |  
 |  Methods defined here:
 |  
 |  __cmp__(...)
 |      x.__cmp__(y) <==> cmp(x,y)
 |  
 |  __contains__(...)
 |      D.__contains__(k) -> True if D has a key k, else False
 |  
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(...)
: (scroll)

1
pydoc也可以不使用任何标志,此时输出将进入less(类似于man)。例如,尝试一下pydoc os.path - dshepherd

10

6
如果您使用 Fedora 操作系统,可以通过运行 yum install python-docs 命令来安装 Python 文档。其他操作系统也可能提供类似的软件包。

3
请执行以下命令来安装 Python 文档并在 Firefox 中打开它:对于 Debian/Ubuntu 操作系统,请输入命令“sudo apt-get install python-doc”,然后运行“firefox /usr/share/doc/python-doc/html/index.html”。 - Dean Serenevy

5

您还可以安装Ipython以在交互模式下检查模块/对象。
例如,您可以在ipython中执行以下操作:

import pygame  
pygame.draw.line?

然后你会得到结果文档:

pygame.draw.line(Surface, color, start_pos, end_pos, width=1): 返回矩形
绘制一条直线段

在ipython中,您可以使用tab键自动完成功能来检查某些内容。


4
最好的方法是阅读内置于Python shell中的文档。
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help()

Welcome to Python 2.7!  This is the online 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/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, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> 

内置帮助的困难之处在于没有帮助函数(我找不到),这使得如果你不知道自己在寻找什么,浏览帮助变得困难。R语言有一个非常好的??topic搜索方法,可以查找所有可用库。Python有类似的东西吗? - naught101

3

我认为最好的选择是使用DevDocs

  • 此外,它可以离线使用
  • 此外,它还有一个桌面应用程序

3

对于 Python < 3.3,请使用以下命令

pydoc -g
python -m pydoc -g

对于Python 3.3及以上版本

pydoc -b
python -m pydoc -b

1
  • 系统 Ubuntu 18.04

要查看Python的离线文档,

  1. 使用sudo apt install python3-doc安装python3-doc。文档将被安装在/usr/share/doc/python3-doc/html
  2. 用浏览器打开/usr/share/doc/python3-doc/html/index.html

这些文档与官方文档站点上的文档一样:https://docs.python.org/3/


-2

4
既然这篇回答没有回答问题,只是提供了一个链接,那么它应该作为一条评论。 - agf
4
当然回答了这个问题,他想要在Linux上阅读文档,而这里有文档。你想让我明确他需要使用浏览器吗? - SpliFF
1
抱歉,这是我的错,我没有说完整。我需要一个离线文档。 - Tanky Woo
1
你假设我们大多数人都在线。但是有许多开发人员在安全设施和其他没有网络连接的地方工作。普及,没错。但并不是到处都有网络。 - Not a machine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接