Get Smarter with Python’s Built-In Help
One of my favorite things about Python is the help()
function. Fire up the standard Python interpreter, and import help
from pydoc
and you can search Python’s official documentation from within the interpreter. Reading the f’ing manual from the interpreter. As it should be1.
The help()
function takes either an object or a keyword. The former must be imported first while the latter needs to be a string like “keyword”. Whichever you use Python will pull up the standard Python docs for that object or keyword. Type help()
without anything and you’ll start an interactive help session.
The help()
function is awesome, but there’s one little catch.
In order for this to work properly you need to make sure you have the PYTHONDOCS
environment variable set on your system. On a sane operating system this will likely be in ‘/usr/share/doc/pythonX.X/html’. In mostly sane OSes like Debian (and probably Ubuntu/Mint, et al) you might have to explicitly install the docs with apt-get install python-doc
, which will put the docs in /usr/share/doc/pythonX.X-doc/html/
.
If you’re using OS X’s built-in Python, the path to Python’s docs would be:
/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/Resources/English.lproj/Documentation/
Note the 2.6 in that path. As far as I can tell OS X Mavericks does not ship with docs for Python 2.7, which is weird and annoying (like most things in Mavericks). If it’s there and you’ve found it, please enlighten me in the comments below.
Once you’ve found the documentation you can add that variable to your bash/zshrc like so:
export PYTHONDOCS=/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/Resources/English.lproj/Documentation/
Now fire up iPython, type help()
and start learning rather than always hobbling along with Google, Stack Overflow and other crutches.
Also, PSA. If you do anything with Python, you really need to check out iPython. It will save you loads of time, has more awesome features than a Veg-O-Matic and notebooks, don’t even get me started on notebooks. And in iPython you don’t even have to import help, it’s already there, ready to go from the minute it starts.
-
The Python docs are pretty good too. Not Vim-level good, but close. ↩
Thoughts?
Please leave a reply:
All comments are moderated, so you won’t see it right away. And please remember Kurt Vonnegut's rule: “god damn it, you’ve got to be kind.” You can use Markdown or HTML to format your comments. The allowed tags are
<b>, <i>, <em>, <strong>, <a>
. To create a new paragraph hit return twice.