Recently I posted a blog about using Emacs and Python together. It was about having setting in your .emacs file allowing Emacs to interact with the default Python and SciPy/NumPy from the Ubuntu repositories, which is out of date. After changing Python version to Anaconda Python this broke all my Emacs setup! It was a real pain to fix so I’ve collected the main ideas here.
To use the latest versions of the Python packages like SciPy and NumPy I downloaded the Anaconda Python distribution. It includes lots of the most useful Python packages for scientific computing by default such as
- Numba and Cython for speeding up code.
- SymPy for symbolic computation.
- F2Py for converting Fortran code.
- IPython for a better interactive experience.
- mpi for parallel computing.
- PEP8 and PyFlakes to enforce coding standards.
I’d really recommend it as an easy way to get a good Python install. You can also pay extra for upgrades like linking NumPy with MKL for enhanced speed (which is free for academics).
Getting a working Emacs setup
Anaconda Python was great from the command line, I’d also edited my .bashrc so that typing python and ipython in bash would bring up the Anaconda interpreters. This was done by adding ‘~/anaconda/bin’ to the $PATH variable. However when using Emacs (which I keep on the Unity launcher) it was still using the standard interpreter and the outdated version of SciPy and NumPy.
After a lot of failed attempts to change the interpreter in Emacs I finally tracked down the cause of the problem: the Ubuntu launch bar doesn’t use the $PATH variable set in .bashrc.
The way I solved this problem was to add some code at the beginning of my .emacs file which sets the $PATH variable within Emacs to match that from .bashrc. It is a modification of the code by sanityinc on this page. If you run OS X then you can get the exec-path-from-shell package from Marmalade or GitHub.
(defun set-exec-path-from-shell-PATH () (interactive) (let ((path-from-shell (replace-regexp-in-string "^.*\n.*shell\n" "" (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'")))) (setenv "PATH" path-from-shell) (setq exec-path (split-string path-from-shell path-separator)))) (set-exec-path-from-shell-PATH)