Linux下面查看python帮助文档

因为python网站被墙,所以在线查看帮助比较困难。好在Linux自身通常都已经带了python的联机帮助,对应的命令是pydoc。


使用方法:

flying-bird@flyingbird:~/examples/python/print_file_type$ pydoc --help
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 -g
    Pop up a graphical interface for finding and serving documentation.

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.

flying-bird@flyingbird:~/examples/python/print_file_type$  

最简单就是第一种:pydoc <name>,其效果和常用的man一样,可以试下如下两个命令,观察使用效果:

pydoc subprocess
pydoc subprocess.Popen

如果对命令行不习惯,可以用pydoc -g,命令行输入这个命令之后,就会启动GUI窗口,如下:

可以在这个search for中输入关键字,比如subprocess,回车就是下面的样子:

选中某一条目,就打开了对应的帮助页面:



注意到,在执行pydoc -g的时候,已经启动了一个pydoc server,端口号为7464。事实上,在pydoc -g命令后出现的第一次窗口中,选择open browser,可以看到下面的窗口:


另外一种就是pydoc -w <name>,示例如下:

flying-bird@flyingbird:~/examples/python/print_file_type$ ll
total 16
drwxrwxr-x 2 flying-bird flying-bird 4096  6月 18 19:28 ./
drwxrwxr-x 8 flying-bird flying-bird 4096  6月 18 21:01 ../
-rwxrwxr-x 1 flying-bird flying-bird  586  6月 18 19:28 print_file_type.py*
-rwxrwxr-x 1 flying-bird flying-bird  586  6月 18 19:27 print_file_type.py~*
flying-bird@flyingbird:~/examples/python/print_file_type$ pydoc -w subprocess
wrote subprocess.html
flying-bird@flyingbird:~/examples/python/print_file_type$ ll
total 56
drwxrwxr-x 2 flying-bird flying-bird  4096  6月 18 21:17 ./
drwxrwxr-x 8 flying-bird flying-bird  4096  6月 18 21:01 ../
-rwxrwxr-x 1 flying-bird flying-bird   586  6月 18 19:28 print_file_type.py*
-rwxrwxr-x 1 flying-bird flying-bird   586  6月 18 19:27 print_file_type.py~*
-rw-rw-r-- 1 flying-bird flying-bird 39516  6月 18 21:17 subprocess.html
flying-bird@flyingbird:~/examples/python/print_file_type$ 

可以打开这个文件:



看到这个,就想到用pydoc subprocess > the_doc.html。但效果如下:

flying-bird@flyingbird:~/examples/python/print_file_type$ pydoc -w subprocess
wrote subprocess.html
flying-bird@flyingbird:~/examples/python/print_file_type$ ll
total 56
drwxrwxr-x 2 flying-bird flying-bird  4096  6月 18 21:17 ./
drwxrwxr-x 8 flying-bird flying-bird  4096  6月 18 21:01 ../
-rwxrwxr-x 1 flying-bird flying-bird   586  6月 18 19:28 print_file_type.py*
-rwxrwxr-x 1 flying-bird flying-bird   586  6月 18 19:27 print_file_type.py~*
-rw-rw-r-- 1 flying-bird flying-bird 39516  6月 18 21:17 subprocess.html
flying-bird@flyingbird:~/examples/python/print_file_type$ pydoc subprocess > the_doc.html
flying-bird@flyingbird:~/examples/python/print_file_type$ ll
total 76
drwxrwxr-x 2 flying-bird flying-bird  4096  6月 18 21:21 ./
drwxrwxr-x 8 flying-bird flying-bird  4096  6月 18 21:01 ../
-rwxrwxr-x 1 flying-bird flying-bird   586  6月 18 19:28 print_file_type.py*
-rwxrwxr-x 1 flying-bird flying-bird   586  6月 18 19:27 print_file_type.py~*
-rw-rw-r-- 1 flying-bird flying-bird 39516  6月 18 21:17 subprocess.html
-rw-rw-r-- 1 flying-bird flying-bird 19738  6月 18 21:21 the_doc.html
flying-bird@flyingbird:~/examples/python/print_file_type$ 

然后打开这个重定向的文件是这个样子:


不过到现在也好理解,因为pydoc subprocess出来的是没有HTML语法的文本,自然没有在浏览器中如预期显示。再者,pydoc为了满足用户潜在的重定向功能,直接就提供了-w选项,保存为html文件,非常方便!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值