Monday, January 20, 2014

Setting up Emacs autocompletion for Python

First read the instructions at 
http://www.drlock.com/blog/2013/01/29/emacs-as-a-python-ide-jedi/ 
-the blog is missing the Emacs el-get step up steps

Use Mr. Batsov's excellent el-get and package instructions.
-Stop when you get the jedi installation.

Then use Mr. Lock's simple jedi installation instructions.

Read step 13!!!!!

Voila, python autocompletion is working.

May need to try Emacs prelude next.

The main problem that I encountered was in emacs after loading the virtualenv and a python file:

ImportError: No module named Jedi
https://github.com/tkf/emacs-jedi/issues/87

M-x epc:pop-to-last-server-process-buffer

Traceback (most recent call last):
  File "/home/--/.emacs.d/elpa/jedi-20130714.1228/jediepcserver.py", line 315, in
    main()
  File "/home/---/.emacs.d/elpa/jedi-20130714.1228/jediepcserver.py", line 311, in main
    jedi_epc_server(**vars(ns))
  File "/home/---/.emacs.d/elpa/jedi-20130714.1228/jediepcserver.py", line 211, in jedi_epc_server
    import_jedi()
  File "/home/---/.emacs.d/elpa/jedi-20130714.1228/jediepcserver.py", line 259, in import_jedi
    import jedi
ImportError: No module named 'jedi'

Lets try cleaning the jedi directory.

[jedi-20130714.1228]$ make clean requirements
rm -rf env
rm -rf elpa
rm -rf *.elc
VIRTUALENV_SYSTEM_SITE_PACKAGES=true virtualenv --python=python env
Already using interpreter /usr/bin/python
Using base prefix '/usr'
New python executable in env/bin/python
Installing setuptools, pip...done.
env/bin/pip install --use-mirrors --requirement requirements.txt
make: env/bin/pip: Command not found
make: *** [requirements] Error 127


 jedi-20130714.1228]$ make requirements
env/bin/pip install --use-mirrors --requirement requirements.txt
make: env/bin/pip: Command not found
make: *** [requirements] Error 127
[jedi-20130714.1228]$ ls
env/  jedi-autoloads.el  jedi-autoloads.el~  jedi.el  jedi.elc  jediepcserver.py  jedi-pkg.el  jedi-pkg.elc  Makefile  requirements.txt
[jedi-20130714.1228]$ ls env/bin
activate  activate.csh  activate.fish  activate_this.py  python*  python3@  python3.3@
[jedi-20130714.1228]$ pwd
/home/----/.emacs.d/elpa/jedi-20130714.1228

The problem is that pip is missing from the jedi virtualenv....

[ jedi-20130714.1228]$ ls ~/.emacs.d/elpa/jedi-20130714.1228env/bin/
activate  activate.csh  activate.fish  activate_this.py  python*  python3@  python3.3@
[ jedi-20130714.1228]$ ls ~/.virtualenvs/startproject/bin/
__pycache__/  activate.csh   activate_this.py  easy_install*      get_env_details*  pip3*    postactivate*    preactivate*    python*   python3.3@
activate      activate.fish  django-admin.py*  easy_install-3.3*  pip*              pip3.3*  postdeactivate*  predeactivate*  python3@

How is pip added to env/bin?


0. Install the required Python tools.
From:  http://www.drlock.com/blog/2013/01/29/emacs-as-a-python-ide-jedi/
  • easy_install jedi
  • easy_install epc
1. Install el-get
Wikemacs El-get
-add the Elisp code at the link to your ~/.emacs
-and execute it :
    M-x eval-current-buffer
-now wait a really long time for El-get to install.

2. Install ELPA if you have not already done so.
Follow the ELPA installation instructions at
http://www.emacswiki.org/emacs/ELPA

3. Add the MELPA repo
To add the repository put this before the call to package-initialize in your init.el file.
(require 'package) 
 
(add-to-list 'package-archives
  '("melpa" . "http://melpa.milkbox.net/packages/") t)
 
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)
 
4. package.el
All fresh Emacses are coming with package.el that simplifies installation of 3rd party packages. auto-complete and ac-dabbrev are available from MELPA package repository. Read install instructions on MELPA site, or about package.el/ELPA in more details

Installation instructions from
http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/

(require 'package)
(package-initialize)
Put this snippet of code near the beginning of your Emacs config, since you’ll definitely want packages installed via package.el to be initalized before you start tweaking them.
With el-get you have access to 536 packages from the get-go, and
all of emacswiki (that’s currently more than 1700 packages) after
you’ve done M-x el-get-emacswiki-refresh. As of writing this comment,
my M-x el-get-package-list contains 2106 packages ready to install,
almost all with descriptions.
5. My ~/.emacs header

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/
;;
;; The magic starts with the command M-x package-list-packages.
;;
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'package)
(require 'package)

(add-to-list 'package-archives
  '("melpa" . "http://melpa.milkbox.net/packages/") t)

(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)

(package-initialize)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://www.emacswiki.org/emacs/ELPA
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                         ("marmalade" . "http://marmalade-repo.org/packages/")
                         ("melpa" . "http://melpa.milkbox.net/packages/")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://wikemacs.org/index.php/El-get
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(add-to-list 'load-path "~/.emacs.d/el-get/el-get")

(unless (require 'el-get nil t)
  (url-retrieve
   "https://raw.github.com/dimitri/el-get/master/el-get-install.el"
   (lambda (s)
     (end-of-buffer)
     (eval-print-last-sexp))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/
;;  instantly deploy packages on any OS/machine
;;
;;  find the packages that you want with
;;  M-x package-list-packages
;;
;;  Add them to the list below
;;
;;  The next time you run emacs they will be added automatically
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar prelude-packages
  '(virtualenv ;; needed by python deps so put it first
    virtualenvwrapper
    python-environment
    epc
    ac-dabbrev
    auto-complete
    erlang
    elpy
    jedi
    jedi-direx
    )
  "A list of packages to ensure are installed at launch.")

;;    flake
;;    pymacs
;;    pytest
;;    pylint
;;    py-import-check
;;    python
;;    python-mode
;;    python-pylint
;;    python-django
;;    python-info
;;    python-magic
;;    pyvirtualenv
;;    scala-mode

(defun prelude-packages-installed-p ()
  (loop for p in prelude-packages
        when (not (package-installed-p p)) do (return nil)
        finally (return t)))

(unless (prelude-packages-installed-p)
  ;; check for new packages (package versions)
  (message "%s" "Emacs Prelude is now refreshing its package database...")
  (package-refresh-contents)
  (message "%s" " done.")
  ;; install the missing packages
  (dolist (p prelude-packages)
    (when (not (package-installed-p p))
      (message "installing package %s\n" p)
      (package-install p))))

(provide 'prelude-packages)
;;; prelude-packages.el ends here

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; https://github.com/kiwanami/emacs-epc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'epc)
(require 'jedi)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Jedi setup
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;Jedi
(autoload 'jedi:setup "jedi" nil t)
(setq jedi:setup-keys t)

(add-hook 'python-mode-hook
(lambda ()
(jedi:setup)
))

;;;;;;
NOTE: Check the messages at the time emacs loads for .emacs errors related to this header. Add your own packages to the list of packages to install.

6. Now open a new emacs
-the mini buffer should show the packages you specified in your ~/.emacs being downloaded and compiled
-All packages should be installed automatically

Note if you see this error there was a network connection error...:
Warning (initialization): An error occurred while loading `/home/---/.emacs':

error: Trying to parse HTTP response code in odd buffer:  *http melpa.milkbox.net:80*

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the `--debug-init' option to view a complete error backtrace.
Then close emacs and reopen.

The elisp installation process will continue.

7. You can see the compilation of the elisp module in the Compile-Log buffer
Compiling file /home/---/.emacs.d/elpa/pymacs-0.25/pymacs-pkg.el at Mon Jan 20 22:36:13 2014
Entering directory `/home/---/.emacs.d/elpa/pymacs-0.25/'

Compiling file /home/---/.emacs.d/elpa/pymacs-0.25/pymacs.el at Mon Jan 20 22:36:13 2014

In pymacs-eval:
pymacs.el:201:21:Warning: `interactive-p' is an obsolete function (as of
    23.2); use `called-interactively-p' instead.

In pymacs-exec:
pymacs.el:211:21:Warning: `interactive-p' is an obsolete function (as of
    23.2); use `called-interactively-p' instead.

8. You can see the contents of the elp directory which contain the elisp code.
ls ~/.emacs.d/elpa
ac-dabbrev-20130906.518/       epc-20130803.2228/                   nose-20110804.619/                python-magic-20120505.1355/
ack-and-a-half-20130815.1917/  erlang-20131025.6/                   popup-20131230.544/               python-mode-6.1.2/
archives/                      find-file-in-project-20120716.1655/  py-import-check-20130802.411/     python-pylint-1.1/
auto-complete-20131128.233/    fuzzy-20131025.2343/                 pylint-20121112.435/              pyvirtualenv-1.1/
concurrent-20130914.536/       highlight-indentation-20131207.835/  pymacs-0.25/                      scala-mode-20131018.1325/
ctable-20131202.2114/          idomenu-20111122.1048/               pytest-0.2.1/                     virtualenv-20120930.1034/
deferred-20130930.607/         iedit-20131021.116/                  python-django-20131214.1841/      yasnippet-20140106.1009/
direx-20130930.1224/           jedi-20130714.1228/                  python-environment-20130624.339/  jedi-direx-readme.txt
elpy-20131109.1120/            jedi-direx-20130525.1039/            python-info-20130916.620/         jedi-readme.txt


9. List the packages in emacs
The magic starts with the command M-x package-list-packages. At this point you should see something in the lines of this.


10. Note: you can click on one of the packages in the installation list and check its installation status and location.
jedi is an installed package.

     Status: Installed in `/home/---/.emacs.d/elpa/jedi-20130714.1228/'.
    Version: 20130714.1228
   Requires: epc-0.1.0, auto-complete-1.4
    Summary: Python auto-completion for Emacs

[back]

11. Now configure virtualenv
i. M-x package-list-packages
ii. Cntl-s virtualenv
iii. Click on the virutalenv link in the package list buffer
iv. Now read the configuration instructions in the virtualenv *Help* buffer.
virtualenv is an installed package.

     Status: Installed in `/home/---/.emacs.d/elpa/virtualenv-20120930.1034/'.
    Version: 20120930.1034
    Summary: Virtualenv for Python

This is a minor mode for setting the virtual environment for the
Python shell using virtualenv and supports both python-mode.el and
python.el. This minor mode was inspired by an earlier
implementation by Jesse Legg and Jeremiah Dodds, however this code
is a complete re-write with a GPLv3 license consistent with
GNU Emacs and python-mode.el.

There are two ways to use virtualenv.
1) The quickest way to get started is to simply type:
     M-x virtualenv-workon
   Which will prompt you to enter the name of a directory in
   ~/.virtualenvs that contains your chosen environment. You can
   hit tab to show the available completions.

   You'll know that you're in virtualenv mode now when you see the
   name of the virtualenv you selected in brackets. So if I were to
   select my turbogears environment that I call tg2.1 then I would
   see [tg2.1] appear in the mode line. To make sure you're new
   python shell is set up correctly you can try running this little
   snippet of python code: Python 3+ code edits in red below

 >>> import os, sys
 >>> print (os.environ)
environ({'EMACS': 't',..)  <<< look for this
 >>> print sys.path
 ['', ...]   <<< look for this
2) The recommended way to use virtualenv minor mode is to use a
.dir-locals.el file in the root of your project directory, however that
requires Emacs 23.1 or higher. There are two buffer-local variables that you
can set for virtualenv as shown in this example:

in file /path/to/project/.dir-locals.el:
((nil . ((virtualenv-workon . "tg2.1")
        (virtualenv-default-directory . "/path/to/project/subdir"))))

The .dir-locals.el is new in Emacs23 and is useful for other
things, too. You should read the dir-locals docs to understand the
format. The variable virtualenv-workon should just be a string the
same as you'd give to the interactive function. The variable
virtualenv-default-directory is useful when you want to have your
python process rooted in a particular directory when it starts, so
that no matter where you are in your project's hierarchy, if you
launch a python shell. This method is recommended because it is
more flexible and will allow multiple virtualenvs running at once
in future versions.

[back]
dir-locals.el notes

Notes:

 virtualenv-workon will now modify Emacs' exec-path as well as the
 shell environment's PATH so that if you run shell-command or launch a
 command in an emacs subprocess it will use any commands it finds in
 your ~/.virtualenvs//bin directory.

 virtualenv-deactivate will restore exec-path and PATH back to their
 original state prior to using the virtualenv-workon command.
virtualenv.el

Emacs manual: Directory-Variables.html


12. Screenshots from the emacs jedi site


13. The magic commands
-add pip jedi epc to the virtualenv 

http://qiita.com/yuu116atlab/items/2a62cb880ac863dcc8ef 
Execute these commands:
cd ~/.emacs.d/elpa/jedi-20130714.1228
virtualenv env
source env/bin/activate
sudo pip install jedi epc sexpdata pep8 pyflakes

 Downloading jedi-0.7.0.tar.gz (136kB): 136kB downloaded
  Running setup.py (path:/home/---/.emacs.d/elpa/jedi-20130714.1228/env/build/jedi/setup.py) egg_info for package jedi
   
    warning: no previously-included files matching '*.pyc' found under directory '*'
Downloading/unpacking epc
  Downloading epc-0.0.5.tar.gz
  Running setup.py (path:/home/---/.emacs.d/elpa/jedi-20130714.1228/env/build/epc/setup.py) egg_info for package epc
   
Downloading/unpacking sexpdata>=0.0.3 (from epc)
  Downloading sexpdata-0.0.3.tar.gz
  Running setup.py (path:/home/---/.emacs.d/elpa/jedi-20130714.1228/env/build/sexpdata/setup.py) egg_info for package sexpdata
   
Installing collected packages: jedi, epc, sexpdata
  Running setup.py install for jedi
   
    warning: no previously-included files matching '*.pyc' found under directory '*'
  Running setup.py install for epc
   
  Running setup.py install for sexpdata
   
Successfully installed jedi epc sexpdata
Cleaning up...


14. Stackoverflow troubleshooting help

15. Trying to parse HTTP response code in odd buffer: http melpa.milkbox.net:80
 https://github.com/purcell/emacs.d/issues/61
Warning (initialization): An error occurred while loading `~/.emacs.d/init.el':
error: Trying to parse HTTP response code in odd buffer: http melpa.milkbox.net:80
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.

https://github.com/purcell/emacs.d/issues/61

16.

17. Python goodies
 http://joat-programmer.blogspot.com/