How to setup Emacs to use a given Python virtualenv?
Asked Answered
P

4

18

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?

Positive answered 22/7, 2016 at 21:27 Comment(0)
J
16

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.

Jaf answered 2/1, 2019 at 16:52 Comment(2)
I couldn't find package to donwload pyvenv using M-x package-installArctogaea
@Arctogaea melpa.org/#/pyvenv You may need to M-x package-refresh-contentsJaf
I
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.

Igal answered 26/8, 2016 at 3:58 Comment(1)
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 github.com/marcwebbie/auto-virtualenv to set the venv based on existence of a .venv directory.Isopropyl
A
4

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

Andersonandert answered 16/12, 2021 at 0:21 Comment(2)
fantastic. this works for when you want to use lsp or eglot instead of elpy.Densimeter
I don't think that these hooks are necessary here, as I don't have them in my configuration and everything works as expected. Nevertheless, this demonstrates nicely the use of some nice elisp constructs, that are useful in the context of pyvenv!Ostyak
I
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.

Isopropyl answered 22/3, 2021 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.