I saw this relevant issue on pyvenv and have added the following code to my
;; 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
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.
pyvenv-workon's value is
nil which could be the issue.
Activating and deactivating the virtual environment through
pyvenv-deactivate works as normal. If I use this method,
which python returns the expected