I have now settled on a solution that uses the virtualenvwrapper ELPA package to activate virtualenvs, allowing emacs-jedi to pick up the virtualenv path from the VIRTUAL_ENV environment variable.
Here is a complete, working, emacs-jedi initialisation:
(defun project-directory (buffer-name)
"Return the root directory of the project that contain the
given BUFFER-NAME. Any directory with a .git or .jedi file/directory
is considered to be a project root."
(interactive)
(let ((root-dir (file-name-directory buffer-name)))
(while (and root-dir
(not (file-exists-p (concat root-dir ".git")))
(not (file-exists-p (concat root-dir ".jedi"))))
(setq root-dir
(if (equal root-dir "/")
nil
(file-name-directory (directory-file-name root-dir)))))
root-dir))
(defun project-name (buffer-name)
"Return the name of the project that contain the given BUFFER-NAME."
(let ((root-dir (project-directory buffer-name)))
(if root-dir
(file-name-nondirectory
(directory-file-name root-dir))
nil)))
(defun jedi-setup-venv ()
"Activates the virtualenv of the current buffer."
(let ((project-name (project-name buffer-file-name)))
(when project-name (venv-workon project-name))))
(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi-setup-venv)
(add-hook 'python-mode-hook 'jedi:setup)
Remember that you have to install virtualenvwrapper first.
Read the virtualenvwrapper documentation for an alternative way of automatically activating project virtual evironments. In short you can create a .dir-locals.el
file in the root of your project, with the following content:
((python-mode . ((project-venv-name . "myproject-env"))))
Change "myproject-env"
to the name of your virtualenv and activate the virtualenvironment using the python-mode
hook:
(add-hook 'python-mode-hook (lambda ()
(hack-local-variables)
(venv-workon project-venv-name)))
(add-hook 'python-mode-hook 'jedi:setup)