16

I use Emacs for a number of tasks and as I am starting to work with Python I would like to keep using Emacs to code in Python.

I have set up a virtualenv for Python3, and it is working as desired. I also have Emacs 24.5 installed with the latest version of Emacs Prelude.

When I edit an Python source file all I expected is working -- code completion, object inspection, etc. -- but for my system wide Python installation, not for the virtual environment I have set up for the project.

How can I tell Emacs to use the virtual environment for a given project?

Jeff
  • 953
  • 2
  • 11
  • 21

4 Answers4

12

I know this is quite an old thread, but for those who come here looking for answer, more up to date answer might be useful.

I use pyvenv, mainly pyvenv-workon.

Give it a try. I hope it works for you.

Mandar Vaze
  • 1,324
  • 13
  • 20
11

Elpy has support for virtual environments built in via Pyvenv. Pyvenv can also be installed as a standalone package. Pyvenv added support for project-specific virtual environments in 2014, but I haven't experimented with them myself so I don't know how well it works.

If you want to code Python in Emacs, I would recommend installing Elpy. It really simplifies the process of getting a good environment up-and-running, and it is modular so you can deactivate sections over time when you decide you want a more tailored package.

You may also want to take a look at virtualenvwrapper.el, although Pyenv looks like it has more functionality. There's also auto-virtualenv, which attempts to automate the virtual environment discovery and activation.

jidicula
  • 3,454
  • 1
  • 17
  • 38
JCC
  • 492
  • 3
  • 17
  • I tried elpy and it kept falling over when trying to auto set up the RPC venv. I didnt have the patience (or knowledge) to stick with its venv handling. I ended up using the excellent https://github.com/marcwebbie/auto-virtualenv to set the venv based on existence of a .venv directory. – RichieHH Mar 22 '21 at 20:26
2

Ok so this post help me with this subject.

Add this to your init.el or ~.emacs:

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

  ;; Set correct Python interpreter
  (setq pyvenv-post-activate-hooks
        (list (lambda ()
                (setq python-shell-interpreter (concat pyvenv-virtual-env "bin/python3")))))
  (setq pyvenv-post-deactivate-hooks
        (list (lambda ()
                (setq python-shell-interpreter "python3")))))

So next time, create the virtual environment in the directory you want (I always call them env and I installed it in the project directory)

python -m venv env

Then open emacs M-x pyvenv-activate RET dir_to_the_environment/env

And finally C-c C-p to run the python subprocess using the environment. All credits to fredrikmeyer

1

The simplest solution for me as a Python beginner, elpy venv didn't work for whatever reason, was to use https://github.com/marcwebbie/auto-virtualenv. I create a venv in my project root, add a .projectile file for good measure and it "just works" when you set up as documented in the github readme.

RichieHH
  • 2,116
  • 4
  • 28
  • 30