My system is macOS and I have both python 2 and 3 installed. I am currently working on a side project for which I want to use Django 1.8 and python 3.4.

I have created a virtual environment with the python 3 command pyvenv venv in the parent folder of my project (I prefer to have my python project folders to be self contained).

At the moment, I cannot get Emacs to recognise and therefore limit all code related assist to the virtual environment.

In addition, is it possible to setup per-project settings such that I can have different projects use different python versions and environment? The ideal scenario will be, when I change into a project directory and start Emacs, it will pick all it's environment settings from that project directory according to some local settings file.

  • I don't know the difference between pyvenv and virtualevn, which I'm familiar with, but can't you just bind python-shell-virtualenv-root to the right path? Either via a file variables or dir variables. E.g. # -*- python-shell-virtualenv-root: "../env" -*-. If this does not work with pyvenv please fill a bug report, M-x report-emacs-bug. – rasmus Jun 28 '15 at 11:22

There is a package called pyvenv to manage virtual environmnets in emacs. Try installing it with

`M-x package-install RET pyvenv RET`

Now You to activate env in your emacs, You can run

M-x pyvenv-activate RET <path-to-venv>

Checkout: https://github.com/jorgenschaefer/pyvenv

For per project management, You can use hooks to activate venv automatically.

  • i have do it, but i can't type pyvenv-activate :-( – zobi8225 Jan 16 '18 at 14:08

I am using https://github.com/jorgenschaefer/pyvenv as well, and I set it up like so:

(use-package pyvenv
  :ensure t
  (pyvenv-mode 1))

This pyvenv-mode 1 part is essential. This will automatically make the venv activate when the variable pyvenv-activate is changed.

So, now leverage .dir-locals.el to set that for my projects.

((nil . ((pyvenv-activate . "~/repos/my_proj/.venv"))))

Now when I open a file in my project, that virtual environment is activated!


The suggestions above work. You can also use a terminal window and activate your virtual environment:

$ source /location/of/virtual/environment/activate

and then open you desired .py file using

$ emacs filename.py

add a & to the end of the command above so you can continue using the terminal while emacs is running. The technique above works for emacs and any other text editor

  • Having to open a new instance of emacs from a shell where you need to manually activate a venv is, while a solution, using a hammer to crack an egg. – RichieHH Apr 2 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.