{"id":861,"date":"2013-07-31T13:50:00","date_gmt":"2013-07-31T18:50:00","guid":{"rendered":"http:\/\/www.alandmoore.com\/blog\/?p=861"},"modified":"2013-07-31T16:59:09","modified_gmt":"2013-07-31T21:59:09","slug":"python-code-completion-in-emacs-at-last","status":"publish","type":"post","link":"https:\/\/alandmoore.com\/blog\/2013\/07\/31\/python-code-completion-in-emacs-at-last\/","title":{"rendered":"Python Code completion in Emacs &#8212; at last!"},"content":{"rendered":"<p>\nI&#8217;ve been coding Python in Emacs for years now, and for the most part it&#8217;s been a satisfactory experience.  After experimenting with various python modes and utilities, I&#8217;ve had a pretty good environment that marries Emacs editing to syntax highlighting, real-time error highlighting, the ipython shell, linting tools like pylint and pep8, and various other goodies.  But the one hole, the one sore spot in the whole works, was code completion.  Something even idle can do out-of-the-box was simply beyond my ability to get functional in Emacs.\n<\/p>\n<p>\nI&#8217;d tried just about every solution the internet could offer:  PyMacs, company-mode, anything-mode, ipython completion, standard autocomplete-mode.  Everything I tried either gave unacceptable results (like autocomplete-mode, which just scans the current buffer for completion information), or just flat-out didn&#8217;t work no matter how many tutorials I followed.\n<\/p>\n<p>\nHere lately I&#8217;ve been trying to work a lot more on my Pythoning, so I decided to see if the state of the art has caught up with this need.  Echo base, I&#8217;ve found it.  Repeat:  I&#8217;ve found it.\n<\/p>\n<p><!--more--><\/p>\n<div id=\"outline-container-sec-1\" class=\"outline-2\">\n<h2 id=\"sec-1\">JEDI<\/h2>\n<div class=\"outline-text-2\" id=\"text-1\">\n<p>\n<a href=\"https:\/\/github.com\/davidhalter\/jedi\">Jedi<\/a> is a generic python code-completion tool, designed to be integrated into whatever editor you like.  The <a href=\"https:\/\/github.com\/tkf\/emacs-jedi\">Jedi.el<\/a> emacs package is used to connect jedi to Emacs.  After a look at the <a href=\"http:\/\/tkf.github.io\/emacs-jedi\/released\/#screenshots\">screenshots for jedi.el<\/a> I was sold; this setup looked darned nice and I needed it.  Unfortunately, as I&#8217;ve learned from experience, these things are never straightforward to install and get working.  After reading through the jedi.el documentation and a couple blog posts, I found it was not so hard after all.  In fact, I&#8217;ve so far managed to get it going on both my main Ubuntu (13.04) system and my Arch Linux test system.\n<\/p>\n<\/div>\n<div id=\"outline-container-sec-1-1\" class=\"outline-3\">\n<h3 id=\"sec-1-1\">Installation<\/h3>\n<div class=\"outline-text-3\" id=\"text-1-1\">\n<p>\nKeeping this as simple as possible, here&#8217;s what I did to install jedi and get it working with Emacs.\n<\/p>\n<\/div>\n<div id=\"outline-container-sec-1-1-1\" class=\"outline-4\">\n<h4 id=\"sec-1-1-1\">Prerequisites<\/h4>\n<div class=\"outline-text-4\" id=\"text-1-1-1\">\n<ul class=\"org-ul\">\n<li>I&#8217;m using Linux (Ubuntu or Arch, so far)\n<\/li>\n<li>Using Emacs 24.3\n<\/li>\n<li>I&#8217;ve<a href=\"http:\/\/marmalade-repo.org\/\"> added the marmalade repository<\/a> to my Emacs package list.\n<\/li>\n<li>I&#8217;ve got python-virtualenv installed.\n<\/li>\n<li>My Emacs is set up to use python-mode whenever I open a .py file.\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"outline-container-sec-1-1-2\" class=\"outline-4\">\n<h4 id=\"sec-1-1-2\">Step one: install jedi.el<\/h4>\n<div class=\"outline-text-4\" id=\"text-1-1-2\">\n<ul class=\"org-ul\">\n<li>Launch Emacs (if for some bizarre reason it&#8217;s ever not running on your system) and do &#8220;M-x package-list-packages&#8221;\n<\/li>\n<li>Search for, and install, the &#8220;jedi&#8221; package.  If you don&#8217;t see it, double check that you&#8217;ve correctly set up your package archives with <a href=\"http:\/\/marmalade-repo.org\/\">marmalade<\/a>.\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"outline-container-sec-1-1-3\" class=\"outline-4\">\n<h4 id=\"sec-1-1-3\">Step two: configure your .emacs<\/h4>\n<div class=\"outline-text-4\" id=\"text-1-1-3\">\n<ul class=\"org-ul\">\n<li>Add, at a minimum, these two lines to your .emacs file:\n<\/li>\n<\/ul>\n<div class=\"org-src-container\">\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n(autoload 'jedi:setup &quot;jedi&quot; nil t)\r\n(add-hook 'python-mode-hook 'jedi:setup)\r\n<\/pre>\n<\/div>\n<ul class=\"org-ul\">\n<li>If you want the default keyboard shortcuts, add this line:\n<\/li>\n<\/ul>\n<div class=\"org-src-container\">\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n(setq jedi:setup-keys t)\r\n<\/pre>\n<\/div>\n<ul class=\"org-ul\">\n<li>If you want it to complete on the &#8220;.&#8221; (so that, when you type some object or module name and a &#8220;.&#8221; it gives you all the possible attributes\/submodules\/methods\/etc), add this line:\n<\/li>\n<\/ul>\n<div class=\"org-src-container\">\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n(setq jedi:complete-on-dot t)\r\n<\/pre>\n<\/div>\n<ul class=\"org-ul\">\n<li>Save your .emacs file.  Exit Emacs.\n<\/li>\n<li>No, really, exit Emacs.  Just doing &#8220;M-x eval-buffer&#8221; doesn&#8217;t cut it for some reason.\n<\/li>\n<li>I know, I know, but you can exit Emacs for just a minute or two.  Maybe have a second computer nearby that you can run Emacs on while you do this.\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"outline-container-sec-1-1-4\" class=\"outline-4\">\n<h4 id=\"sec-1-1-4\">Step three: configure jedi&#8217;s virtualenv<\/h4>\n<div class=\"outline-text-4\" id=\"text-1-1-4\">\n<p>\nOne cool thing about the way jedi.el works is that it creates its own Python virtualenv to operate in, so that you don&#8217;t have to go to the trouble of installing jedi in your main system environment.  This is nice since there aren&#8217;t packages in most distro&#8217;s repositories for jedi (yet).  Package.el doesn&#8217;t set this up for you, though, so you have to do this:\n<\/p>\n<ul class=\"org-ul\">\n<li>Browse to the folder containing jedi.el.  It&#8217;s probably in ~\/.emacs.d\/elpa\/jedi-(someversionnumber).  If not, it&#8217;s wherever package.el downloads packages on your system.\n<\/li>\n<li>Note there&#8217;s a &#8220;Makefile&#8221; in this directory.\n<\/li>\n<li>Run &#8220;make requirements&#8221;.  This will create a virtual environment in the plugin&#8217;s folder with jedi and all its dependencies.\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"outline-container-sec-1-1-5\" class=\"outline-4\">\n<h4 id=\"sec-1-1-5\">Step four: Check.it.out.<\/h4>\n<div class=\"outline-text-4\" id=\"text-1-1-5\">\n<ul class=\"org-ul\">\n<li>Relaunch Emacs.  There, that wasn&#8217;t so bad was it?\n<\/li>\n<li>Open a new file with a .py extension, so as to trigger Python mode.\n<\/li>\n<li>Try importing some standard module like &#8220;os&#8221;.  You should right away see some auto completion as you type.\n<\/li>\n<li>Try typing &#8220;os.&#8221; and notice you get a pop-up with the contents of the &#8220;os&#8221; module.\n<\/li>\n<li>Now type &#8220;os.path&#8221;.  Put your cursor somewhere on the word &#8220;path&#8221; and do &#8220;M-x jedi:show-doc&#8221; (or, if you loaded the default keys, C-c d).  You should have a buffer pop up with the documentation for os.path.\n<\/li>\n<\/ul>\n<p>\nPretty awesome stuff, right???\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-sec-1-2\" class=\"outline-3\">\n<h3 id=\"sec-1-2\">Things I&#8217;m still working through<\/h3>\n<div class=\"outline-text-3\" id=\"text-1-2\">\n<p>\nSo far the only issue I&#8217;m having with Jedi is that it wants to complete using Python2 on my Ubuntu system (which makes sense, as it&#8217;s the default Python on 13.04).  On the Arch system, where Python3 is default, it completes for Python3.  It&#8217;d be nice if I could figure out a simple way to switch this easily, since I often need to work in both environments.\n<\/p>\n<p>\nOtherwise, I&#8217;m pretty pleased with Jedi so far.  Makes we want to write more code!\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The author is at last a Jedi.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7,5],"tags":[12,20,22,28,47],"class_list":["post-861","post","type-post","status-publish","format-standard","hentry","category-floss","category-programming","category-technology","tag-emacs","tag-how-to","tag-instructional","tag-programming-2","tag-python"],"_links":{"self":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/comments?post=861"}],"version-history":[{"count":3,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/861\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/861\/revisions\/867"}],"wp:attachment":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/media?parent=861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/categories?post=861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/tags?post=861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}