Emacs and Anaconda Python

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.

Anaconda Python

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 solution

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 ()
        (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))))



7 thoughts on “Emacs and Anaconda Python

  1. With havin so much content do you ever run into any problems of plagorism or copyright violation?
    My website has a lot of unique content I’ve either written myself or
    outsourced but it seems a lot of it is popping it up all over the web without my permission.
    Do you know any methods to help reduce content from being ripped off?
    I’d definitely appreciate it.

  2. Sam,
    thanks for the emacs/anaconda fix. Worked first time. Saved me time. Thank you for taking the time you took to post it.

    Best Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s