I try to use virtualenvwrapper.el with the build-in python.el and my virtual environments in a ./.venv directory inside each project.

Because all virtual envs have the same name .venv I cannot make a list of all venvs in venv-location. What I do instead is, when I want to work in a particular environment (say ~/my/project/.venv), I set:

(setq venv-location (list "~/my/project/.venv"))
(venv-workon (venv-dir-to-name "~/my/project/.venv")))

This works as expected for instantiating the inferior Python process via M-x run-python: This process runs in the virtual env ~/my/project/.venv.

However because venv-workon seems to change Emacs' general PATH variable, normal swells (started with M-x shell) are affected too: They all get the virtual env~/my/project/.venv`. But I do not want this for my normal shells.

How can I manage my Python virtual environments for python.el's Python processes , but not for shell and all other processes? Do other libraries (like pyenv) handle this as I want it?(*)

(*) I had tried pyvenv earlier and if I remember it right I had the same issue: Normal shell get thesame virtual env as my python project.

  • I have never used a virtual environment wrapper .... I have, however, set the process-environment on a buffer-local basis and modified the path for just the current buffer, e.g., an eshell buffer; or, to generate a LaTeX PDF file from a *.tex buffer, etc. E.g., (set (make-local-variable 'process-environment) (mapcar 'concat process-environment)) and (setenv "PATH" (concat "/absolute/path/to/folder/of/executables:" (getenv "PATH"))) -- note the colon ...
    – lawlist
    May 5 '20 at 4:19
  • @lawlist That looks like a pretty cool idea! I wasn't aware that you can make the process-environment buffer-local. Not sure though how to integrate this with python.el/virtualenvwrapper.el...
    – halloleo
    May 5 '20 at 4:39
  • I have not attempted to tweak or fix virtual environment wrapper, but I thought perhaps providing an example of how to create a subprocess with a custom/temporary PATH (that does not permanently alter/clobber the global process-environment) might help some: gist.github.com/lawlist/6b9da6e3199e2a5ebf581655b3c0ba4c
    – lawlist
    May 6 '20 at 3:42

Your Answer

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

Browse other questions tagged or ask your own question.