Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyvenv-mode doesn't deactivate the virtualenv in buffers where pyvenv-activate isn't unset #124

Open
xenophonf opened this issue Dec 4, 2023 · 0 comments

Comments

@xenophonf
Copy link

I want to be able to open my Python projects in Emacs and automatically set up a virtual environment—but just for project-related buffers! To accomplish that, I've enabled the pyvenv minor mode by customizing the Emacs Lisp variable pyvenv-mode, and I've put the following code into my Python project's .dir-locals.el:

((nil . ((eval . (progn
                   ;; install or activate the development environment                                                                                                                        
                   ;; (requires pyvenv-tracking-mode)                                                                                                                                        
                   (set (make-local-variable 'my-project)
                        (locate-dominating-file default-directory ".dir-locals.el"))
                   (set (make-local-variable 'my-project-venv)
                        (concat my-project ".venv"))
                   (if (not (file-exists-p my-project-venv))
                       (let ((cwd default-directory))
                         (cd my-project)
                         (async-shell-command "make dev-infra")
                         (cd cwd)
                         (message "Please re-open this file/directory after the \"make dev-infra\" command finishes."))
                     ;; must be set project-wide to pre-commit work                                                                                                                          
                     (set (make-local-variable 'pyvenv-activate)
                          my-project-venv))))))
 (python-mode . ((eval . (progn
                           ;; sort imports, then style code                                                                                                                                  
                           (add-hook 'before-save-hook #'py-isort-before-save nil t)
                           (add-hook 'before-save-hook #'elpy-black-fix-code nil t))))))

It works beautifully. I can open files and folders in one Python project directory, and my directory-local variables automatically install my virtual environment or, if it's already installed, activate it. Opening files/folders related to another project automatically switches to the second project's virtual environment. Smooth as silk.

But if I want to open files/folders outside a project directory, i.e., in buffers where the local variable pyvenv-activate isn't set, I must manually run pyvenv-deactivate.

How do I configure pyvenv-mode to deactivate a virtual environment outside of the relevant buffers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant