{"id":4,"date":"2007-10-28T22:14:25","date_gmt":"2007-10-29T05:14:25","guid":{"rendered":"https:\/\/blog.alienoverlord.com\/?p=4"},"modified":"2008-01-09T19:05:51","modified_gmt":"2008-01-10T02:05:51","slug":"snakes-and-cats-tab-completion-in-python-and-ipython-on-leopard","status":"publish","type":"post","link":"https:\/\/blog.alienoverlord.com\/?p=4","title":{"rendered":"Snakes and Cats. Tab-completion in Python and iPython on Leopard"},"content":{"rendered":"<p>[UPDATE]: The latest builds of iPython have a fix for tab-completion built-in, so the patch mentioned below isn&#8217;t necessary.<\/p>\n<p>\nPython users who are running Mac OS X 10.5, aka Leopard, will be pleased to find that Leopard ships with Python version 2.5.1, and comes with a host of wonderful things. \u00c2\u00a0Especially in the PyObjC realm, something I&#8217;ll cover in another post. \u00c2\u00a0 Tiger had a rather lame build of Python 2.3 installed by default which had no readline support. <!--more--><br \/>\nReadline is a gnu library which provides command-line editing, history, and tab-completion services, and it (or something like it) is pretty much essential for doing any real work in the python interpreter. \u00c2\u00a0 One of the first things I&#8217;d do when setting up a new Tiger system is to install the pythonmac packages from pythonmac.org, which were compiled with that readline support.\u00c2\u00a0<BR\/><BR\/>On Leopard, \u00c2\u00a0Python does have command-line editing, courtesy of &#8220;editline&#8221;, which performs a mostly identical service to readline. \u00c2\u00a0However: \u00c2\u00a0Tab completion in the python interpreter isn&#8217;t working. \u00c2\u00a0(if you don&#8217;t know, tab-completion is a really handy feature where you type part of a command, hit &#8216;tab&#8217;, and the rest of the word you were trying to type is filled in. It saves you tons of typing and typing errors. \u00c2\u00a0With something like Python, you can use it to help you remember method names in classes:<BR\/><BR\/><code>python<\/code><BR\/><code>&gt;&gt;&gt; import os<\/code><BR\/><code><\/code><BR\/><code>&gt;&gt;&gt; os.&lt;hit tab key here&gt;<\/code><BR\/><code>Display all 235 possibilities? (y or n)<\/code><BR\/><BR\/>&#8216;y&#8217; will get you a big ol&#8217; list of all os&#8217;s methods and attributes. \u00c2\u00a0It&#8217;s _really_ handy. \u00c2\u00a0 But it weren&#8217;t workin&#8217;! \u00c2\u00a0 What&#8217;s more, I often use iPython, and on Tiger somehow I got tab-completion for free (maybe because I&#8217;d built and installed readline?), \u00c2\u00a0but not on Leopard. <BR\/><BR\/>Happily, there are user communities for this kind of thing, and Python user communities are active and usually very helpful. \u00c2\u00a0No sooner was Leopard &#8220;out in the wild&#8221;, then folks on the pythonmac-SIG mailing list had a solution, which I will repeat here in my own weird convoluted way&#8230;<BR\/>Here is the thread: <a href=\"http:\/\/mail.python.org\/pipermail\/pythonmac-sig\/2007-October\/019289.html\">http:\/\/mail.python.org\/pipermail\/pythonmac-sig\/2007-October\/019289.html<\/a><BR\/><BR\/>To get this to work with the python interpreter, you could simply add a line to a .pythonstartup file (and set your PYTHONSTARTUP environment variable).  If you don&#8217;t already have a startup file, create one and set the PYTHONSTARTUP variable in your .profile or .bash_profile or whatever shell rc file you use. \u00c2\u00a0 I use bash, so I added the following to my .bash_profile file:<BR\/><BR\/><code># Special startup .rc file for python<\/code><BR\/><code>PYTHONSTARTUP=$HOME\/.pythonstartup<\/code><BR\/><code>export PYTHONSTARTUP<\/code><BR\/><BR\/>Since my PYTHONSTARTUP file is named &#8220;.pythonstartup&#8221;, I open that file and add:<BR\/><code>import rlcompleter<\/code><BR\/><code>import readline<\/code><BR\/><code>readline.parse_and_bind(\"bind ^I rl_complete\")<\/code><BR\/><BR\/>Then I quit and relaunch my terminal (or &#8216;source .bash_profile&#8217;) to pick up the PYTHONSTARTUP environment change, and we have tab-completion in Python&#8217;s interpreter.  Yay!<BR\/><BR\/>But what about iPython?   So one way is to patch your local copy of iPython, although I suspect it will be patched fairly soon anyway.  If you use iPython on Leopard, check out the link above (pythonmac-SIG mailing list).  One of the members of that list posted a patch &#8212; I hope he doesn&#8217;t mind if I attach it here&#8230;<a href=\"https:\/\/blog.alienoverlord.com\/wp-content\/uploads\/2007\/10\/ip_leoparddiff.tbz\" title=\"iPython patch to fix tab-completion\">iPython patch to fix tab-completion<\/a><BR\/><BR\/>If you don&#8217;t like the idea of patching your iPython install, just add the above 3 code lines to your .ipython\/ipy_user_conf.py file, but keep in mind this is only for Leopard.<BR\/><BR\/>All in all, Leopard is going to be great for Python coding.  The bridge support features with PyObjC are sooo cooool.  Most of the really useful frameworks on Mac OS X are &#8220;bridged&#8221; so you can import them and start using them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[UPDATE]: The latest builds of iPython have a fix for tab-completion built-in, so the patch mentioned below isn&#8217;t necessary. Python users who are running Mac OS X 10.5, aka Leopard, will be pleased to find that Leopard ships with Python version 2.5.1, and comes with a host of wonderful things. \u00c2\u00a0Especially in the PyObjC realm, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,7],"tags":[12,10,8,35,9,11],"_links":{"self":[{"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=\/wp\/v2\/posts\/4"}],"collection":[{"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4"}],"version-history":[{"count":0,"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=\/wp\/v2\/posts\/4\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alienoverlord.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}