Linux下面查看python帮助文档

来源:互联网 发布:mac如何关掉dashboard 编辑:程序博客网 时间:2024/04/27 20:27

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


使用方法:

flying-bird@flyingbird:~/examples/python/print_file_type$ pydoc --helppydoc - the Python documentation toolpydoc <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 subprocesspydoc 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$ lltotal 16drwxrwxr-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 subprocesswrote subprocess.htmlflying-bird@flyingbird:~/examples/python/print_file_type$ lltotal 56drwxrwxr-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.htmlflying-bird@flyingbird:~/examples/python/print_file_type$ 

可以打开这个文件:



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

flying-bird@flyingbird:~/examples/python/print_file_type$ pydoc -w subprocesswrote subprocess.htmlflying-bird@flyingbird:~/examples/python/print_file_type$ lltotal 56drwxrwxr-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.htmlflying-bird@flyingbird:~/examples/python/print_file_type$ pydoc subprocess > the_doc.htmlflying-bird@flyingbird:~/examples/python/print_file_type$ lltotal 76drwxrwxr-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.htmlflying-bird@flyingbird:~/examples/python/print_file_type$ 

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


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


0 0
原创粉丝点击