I am using pyvenv with doom and am trying to have my python virtual environments automatically loaded when I enter a certain project.
I saw this relevant issue on pyvenv and have added the following code to my config.el
;; autoload python virtual environments
;; https://github.com/jorgenschaefer/pyvenv/issues/51#issuecomment-474785730
(defun pyvenv-autoload ()
"Activates pyvenv version if .venv directory exists."
(f-traverse-upwards
(lambda (path)
(let ((venv-path (f-expand ".venv" path)))
(if (f-exists? venv-path)
(progn
(pyvenv-workon venv-path))
t)))))
(add-hook 'python-mode-hook 'pyvenv-autoload)
I expect that whenever I enter a python file inside a project with a .venv directory, it would automatically activate the appropriate virtual environment. However, running M-x shell-command
RET which python
returns /usr/bin/python
when I would expect it to return something like /home/jacob/src/beets/.venv/bin/python
Some debugging:
python-mode-hook
's value is
(er/add-python-mode-expansions
doom--setq-tab-width-for-python-mode-h
+python-use-correct-flycheck-executables-h
evil-collection-python-set-evil-shift-width
doom-modeline-env-setup-python
pyvenv-autoload
pipenv-mode)
Which looks good to me.
However,
pyvenv-workon
's value is nil
which could be the issue.
Activating and deactivating the virtual environment through pyvenv-activate
and pyvenv-deactivate
works as normal. If I use this method, which python
returns the expected /home/jacob/src/beets/.venv/bin/python