44

In python I can get some rudimentary documentation for any object using help(<object>). But to be able to search the documentation, I have to go online. This isn't really helpful if I'm somewhere where the internet isn't accessible.

In R, there is a handy double question mark feature (??<topic>) that allows me to search through the documentation of all installed libraries for any function that includes <topic> in its name or documentation string. Is there anything similar for python? Perhaps even just for loaded objects?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
naught101
  • 18,687
  • 19
  • 90
  • 138
  • What platform/operating system? – wwii Oct 18 '13 at 04:01
  • @wii: I'm on linux, but a cross-platform answer would be preferable, if possible. – naught101 Oct 18 '13 at 04:02
  • 4
    Note that http://stackoverflow.com/questions/723017 points to http://docs.python.org/3/download.html which is certainly useful, but only includes core python docs. I'm interested in searching the docs for python core *and* all installed modules (or just the imported ones). – naught101 Oct 18 '13 at 04:16
  • Would be really interesting in the reason for the downvotes here. If such a feature is available in other languages (like R), then is it somehow not applicable to python? Unpythonic? Against the Zen? – naught101 Nov 21 '13 at 12:51
  • On many modules, you can go through the files and figure it out by reading the comments or code. –  Nov 24 '13 at 00:14
  • 1
    @AHuman: I could also pull a car apart to figure out how it works, but if I want to fix something, I'd prefer to have a decent service manual with a good index. – naught101 May 22 '14 at 04:07
  • @naught101 Yes it is not the best way. However, all you need do is look at the file and read the comments at the start of each function or class. Then again, you can only do this when the module is only one file. Usually when there are more than one, there will be a readme file with some description. Basically, you are only looking at the labels of each major part of the car. It frankly is not too bad if nothing else works. –  May 22 '14 at 05:30

9 Answers9

32

pydoc comes with python and can do searches but only in the synopsis lines of available modules. Quoting pydoc --help:

pydoc -k 
    Search for a keyword in the synopsis lines of all available modules.

Note that into pydoc you can perform searches using "/".

Nicolas Barbey
  • 6,639
  • 4
  • 28
  • 34
  • That sounds like just about exactly what I'm searching for, except that it fails badly when I use it: `AttributeError: 'NoneType' object has no attribute 'load_module'`. I think perhaps I have a module somewhere with problems, but I can't find it, and presumably such a tool should handle that kind of problem. Also, it looks like it loads all modules every time, which takes a while: It would be awesome if it cached the results (maybe it does and I'm just not seeing it, because it's failing). – naught101 Nov 28 '13 at 12:39
  • 1
    Indeed pydoc works by importing the modules. If you have one in your python path that cannot be imported, it will fail. – Nicolas Barbey Nov 28 '13 at 13:01
  • 2
    You can also browse in a browser with `pydoc3 -b` which will `Start an HTTP server on an arbitrary unused port and open a Web browser to interactively browse documentation.` – Mark Haferkamp Apr 18 '19 at 11:48
31

Look in the python folder in the folder: Doc. This folder has the entire downloaded documentation of the python docs from python.org. I know this is a VERY late answer, but it brings up an easy solution.

  • 1
    The question is about *searching* the docs. What's your proposal? I guess grep might work, depending on the format.. – naught101 Nov 27 '14 at 00:37
  • 2
    @naught101 Well, there is the option to search in the documentation window. It is, as my answer says, the same documentation as on the site. –  Nov 27 '14 at 03:58
  • You're right, I missed that at the time. However, it doesn't include thrid-party libraries, like numpy. They have separate docs available, but you can't search across the entire ecosystem at once... – naught101 Jan 30 '15 at 03:45
  • I actually think this is the best answer now - the HTML versions of the python docs are quite good, and the search works pretty well. However, it seems that if there's no python-[module]-doc package in your package manager (e.g. for less popular packages), then there's no easy way to install a packages' docs and use it in this way. – naught101 Mar 20 '15 at 00:14
  • 1
    I still agree with the first comment. This answer is incomplete - more of a hint in the right direction. In my case, for example, I'm not even sure where the Doc folder is or how to find it. Also, what command to use to search it? I could figure out something using grep, I suppose. I think it could benefit from an example code snippet. – argentum2f Jan 30 '18 at 18:28
  • Where is 'Doc' folder? – user3804598 Apr 28 '20 at 13:29
  • @user3804598 for me it's in the main Python27 folder C:\Python27\Doc –  May 06 '20 at 20:53
  • @user3804598 it should be in the main Python folder. I'm not sure where that is for Linux, but maybe in the root directory or a programs folder? –  May 11 '20 at 17:46
  • 1
    @AHuman thanks. I found it, on Linux on my machine, it is /usr/lib/python3.8/site-packages/numpy/doc (example for numpy package) – user3804598 May 16 '20 at 15:06
8

This was mentioned in the comments already: Zeal is similar to Dash but for Windows/Linux. It uses the same sources as Dash. It's built using Qt and is available in the repositories for several distros, for Ubuntu there is a PPA. Download it here.

Zeal is a simple offline API documentation browser inspired by Dash (OS X app), available for Linux and Windows.

  • Quickly search documentation using Alt+Space (or customised) hotkey to display Zeal from any place in your workspace.
  • Search in multiple sets of documentation at once.
  • Don't be dependent on your internet connection.
  • Integrate Zeal with Emacs, Sublime Text, or Vim. See Usage » Editor plugins for details.

It is open source (GPL), development happens on GitHub. Zeal uses the same stylesheets/HTML as the online docs, so everything should look familiar.

An in-browser alternative is devdocs.io. You can access the website even if you are offline, provided that you've marked them for local offline storage. You'll need to enable the Python 2 docs, and then mark them for offline storage here. However, as a longtime user of the online Python docs, I find the custom stylesheet that DevDocs uses a bit distracting.

Community
  • 1
  • 1
jmiserez
  • 2,991
  • 1
  • 23
  • 34
5

Just to add another option for offline access of python docs (mostly core):

I don't have access to a linux computer at the moment, but on windows, you can navigate to your_python_dist_folder/doc to find some help files. Particularly python275.chm for instance.

If there's no doc folder on your linux machine, you can download the file here and google for a linux chm viewer:

https://www.google.com/search?q=linux+chm+viewer

::Note:

Some distributions also include docs for other packages in there... might be worth a check. Other than that, help(module) usually returns good information.

Edit:

You could get something that might be a little closer to what you want by using pydoc. E.g. you are looking for something about sin in the math module:

import math
import pydoc
[i for i in dir(math) if 'sin' in pydoc.getdoc(getattr(math,i))]

This would return the methods whose docstrings include sin:

['acos', 'acosh', 'asin', 'asinh', 'cos', 'cosh', 'isinf', 'sin', 'sinh']

for which you then could run the help() function

pandita
  • 4,739
  • 6
  • 29
  • 51
4

In case your working in a Mac there is Dash, which allows you to download docsets and then explore/search offline. Despite its documentation functionality, Dash is also a Snippet Manager.

simme
  • 41
  • 2
  • 1
    do you know if there a open source alternative to Dash? The state of offline documentation browsing is abysmal and I'm considering writing one myself. – lsh Dec 01 '13 at 15:05
2

Windows Idle - F1 from shell window or editing window gets you a windows help file of all the docs. I think it's better than the online version - it's easier to find stuff.

wwii
  • 23,232
  • 7
  • 37
  • 77
2

Although there are certainly better documentations built into your computer than help() like windows idle, another option for some of the more common topics would just be to save some of the online documentation to your computer. For the modules you use a lot and want to access offline, you could just download a text file version of the official online python documentation, which is the best place to get documentation. (file > save page as > select .txt file format)

trevorKirkby
  • 1,886
  • 20
  • 47
2

This may not have been available at the time the question asked and answered, but python.org now makes all the documentation available online as an archive of HTML files which can be navigated and searched offline: https://docs.python.org/2/download.html

(The link directs to docs for the latest version of 2.x, but you can choose 3.x and older 2.x versions from that page)

sabeer
  • 31
  • 3
0

You should try ipython.

object_name? will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes.

The magic commands %pdoc, %pdef, %psource and %pfile will respectively print the docstring, function definition line, full source code and the complete file for any object (when they can be found). If automagic is on (it is by default), you don’t need to type the ‘%’ explicitly.

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
  • 1
    Sorry, but that's totally irrelevant to the question. I know how to read the docs of an object, and I already use ipython. But none of that allows me to **search** the documentation for all available modules, or all loaded objects. – naught101 Oct 18 '13 at 03:41