I'm trying to use .dir-locals.el within porjects to specify the Python virtualenv that is activated on a per project basis using pyvenv. I've read the examples at Directory Variables - GNU Emacs Manual and have tried

((python-mode . ((pyvenv-workon "~/.virtualenvs/default")
         (subdirs . nil))))

With this I get the following error...

File local-variables error: (wrong-type-argument stringp (~/virtualenvs/default)

...and it does not activate this virtual environment, instead its activating anaconda3 which is a nuisance because I thought I'd purged my system of Anaconda (in favour of using virtualenvwrapper).

I have the following in ~/.emacs.d/settings/python-settings.el (which is called from ~/.emacs.d/init.el)

;; elpy configuration
(pyvenv-activate "~/.virtualenvs/dskit")

;; Set ipython as the default interpreter
(setq elpy-rpc-python-command "python3")
(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "-i --simple-prompt")

...and as I say I've purged my system of anaconda so I'm not sure how or why its being picked up as a virtual environment. Is there anyway to get debugging information out of Emacs pyvenv module when its loading to find out what its doing co I can work out whats going on?

1 Answer 1


One of the confusing things about .dir-locals.el variables is that the bindings need to be conses, usually written in dotted form:

((python-mode . ((pyvenv-workon . "~/.virtualenvs/default")
                 (subdirs . nil))))

Without the ., lisp interprets the second part of the pair as a list, so (in this case), your configuration was saying that the value of pyvenv-workon was the list ("~/.virtualenvs/default") instead of just the string, which is what the wrong-type-argument error is saying. If the bit about cons is unfamiliar, you might take a look at cons in the Emacs Lisp manual.

  • Thank you @Win that has done the trick. Slowly learning more Lisp as I get more into using Emacs for my IDE and all other aspects of my work on computers.
    – slackline
    Dec 17, 2018 at 13:02
  • For some reason this makes my Python buffer extremely laggy - I have (setenv "WORKON_HOME" "~/<<directory where envs are stored>>")) in my config, and putting ((python-mode . ((eval . (pyvenv-workon "<<env name>>"))))) in .dir-locals.el fixed it for me.
    – Nazaal
    Apr 8, 2023 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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