Use pyvenv exclusively to manage virtualenvs
This commit is contained in:
parent
d87c681d02
commit
8f2ec38efe
@ -2957,10 +2957,15 @@ Run black on the current buffer:
|
||||
Support pyvenv within Emacs:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(use-package pyvenv
|
||||
:defer 0
|
||||
:commands (pyvenv-mode
|
||||
pyvenv-tracking-mode
|
||||
pyvenv-workon
|
||||
pyvenv-activate
|
||||
pyvenv-track-virtualenv))
|
||||
pyvenv-track-virtualenv)
|
||||
:config
|
||||
(pyvenv-mode
|
||||
pyvenv-tracking-mode))
|
||||
|
||||
(defun eshell/workon (name)
|
||||
(pyvenv-workon name))
|
||||
@ -2972,76 +2977,18 @@ Support pyvenv within Emacs:
|
||||
(pyvenv-deactivate))
|
||||
#+END_SRC
|
||||
|
||||
Pipenv is a dependency management/virtual environment tool. pipenv.el teaches Emacs its ways:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(use-package pipenv
|
||||
:commands (pipenv-mode
|
||||
pipenv-activate
|
||||
pipenv-run)
|
||||
:custom
|
||||
(pipenv-with-projectile nil)
|
||||
(pipenv-with-flycheck nil))
|
||||
#+END_SRC
|
||||
|
||||
And support pyenv (NOT pyvenv) to change Python versions:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(use-package pyenv-mode
|
||||
:defer t)
|
||||
#+END_SRC
|
||||
|
||||
Add a hook to set the correct python for the current buffer:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(defvar python-buffer-venv nil)
|
||||
(make-variable-buffer-local 'python-buffer-venv)
|
||||
|
||||
(defun save-buffer-venv (&rest args)
|
||||
(setq python-buffer-venv pyvenv-virtual-env))
|
||||
|
||||
(advice-add 'pyvenv-activate :after #'save-buffer-venv)
|
||||
(advice-add 'pyvenv-workon :after #'save-buffer-venv)
|
||||
(advice-add 'pyvenv-track-virtualenv :after #'save-buffer-venv)
|
||||
|
||||
(defun activate-buffer-venv ()
|
||||
"Activates the virtualenv that was previously set in this buffer."
|
||||
(when (and python-buffer-venv
|
||||
(not (equal python-buffer-venv pyvenv-virtual-env)))
|
||||
(pyvenv-activate python-buffer-venv)))
|
||||
|
||||
(defun maybe-activate-venv ()
|
||||
"Activates the virtual env for the current buffer if one exists.
|
||||
Returns `t' if a venv was activated."
|
||||
(interactive)
|
||||
(cond
|
||||
((pipenv-activate) t)
|
||||
((or pyvenv-activate pyvenv-workon)
|
||||
(progn (pyvenv-track-virtualenv) t))))
|
||||
|
||||
(defun activate-venv-post-command ()
|
||||
(ignore-errors (activate-buffer-venv)))
|
||||
|
||||
(define-minor-mode python-env-mode
|
||||
"Global minor mode to track Python environment."
|
||||
:global t
|
||||
(cond
|
||||
(python-env-mode
|
||||
(add-to-list 'mode-line-misc-info '(python-env-mode pyvenv-mode-line-indicator))
|
||||
(add-hook 'hack-local-variables-hook #'maybe-activate-venv)
|
||||
(add-hook 'post-command-hook #'activate-venv-post-command))
|
||||
((not python-env-mode)
|
||||
(setq mode-line-misc-info (delete '(python-env-mode pyvenv-mode-line-indicator)
|
||||
mode-line-misc-info))
|
||||
(remove-hook 'hack-local-variable-hook #'maybe-activate-venv)
|
||||
(remove-hook 'post-command-hook #'activate-venv-post-command))))
|
||||
|
||||
(add-hook 'after-init-hook #'python-env-mode)
|
||||
#+END_SRC
|
||||
|
||||
Use the LSP python client:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(defun python-lsp ()
|
||||
"Activates the project virtualenv if one exists then runs LSP."
|
||||
(interactive)
|
||||
(maybe-activate-venv)
|
||||
(pyvenv-track-virtualenv)
|
||||
(lsp-deferred))
|
||||
|
||||
(add-hook 'python-mode-hook #'python-lsp)
|
||||
|
Loading…
Reference in New Issue
Block a user