February 3, 2010

Mimicing source virtualenv/bin/activate in Emacs.

For a while now I've had a problem where at work we rely heavily on virtualenv and I occasionally want to do things like run pyflakes or run trial from the Twisted installed in that virtualenv. This following activate-virtualenv function appears to be all that is necessary to mimic source virtualenv/bin/activate for Emacs. Enjoy.


(defun add-to-PATH (dir)
"Add the specified path element to the Emacs PATH"
(interactive "DEnter directory to be added to PATH: ")
(if (file-directory-p dir)
(setenv "PATH"
(concat (expand-file-name dir)
path-separator
(getenv "PATH")))))

(defun activate-virtualenv (dir)
(setenv "VIRTUAL_ENV" dir)
(add-to-PATH (concat dir "/bin"))
(add-to-list 'exec-path (concat dir "/bin")))


Updated: I forgot to add my add-to-PATH implementation.

1 comments:

pygabriel said...

Thank you very much for the hint, it works like a charm and it's a must to have!