feat(python): add uv virtual environment support

This commit is contained in:
Damian Barabonkov 2025-11-30 23:33:25 +01:00
parent ead254e152
commit beed37ad83
5 changed files with 47 additions and 0 deletions

View file

@ -30,6 +30,8 @@ This module adds [[https://www.python.org/][Python]] support to Doom Emacs.
support via [[https://python-poetry.org/][Poetry]].
- +pyenv ::
Enable Python virtual environment support via [[https://github.com/pyenv/pyenv][pyenv]]
- +uv ::
Enable Python virtual environment support via [[https://github.com/astral-sh/uv][uv]]
- +pyright ::
Add support for the pyright LSP server (requires [[doom-module:+lsp]]).
- +tree-sitter ::
@ -44,6 +46,7 @@ This module adds [[https://www.python.org/][Python]] support to Doom Emacs.
- [[doom-package:pip-requirements]]
- [[doom-package:poetry]] if [[doom-module:+poetry]]
- [[doom-package:pyenv]]
- [[doom-package:uv-mode]] if [[doom-module:+uv]]
- [[doom-package:pyimport]]
- [[doom-package:py-isort]]
- [[doom-package:python-pytest]]
@ -74,6 +77,7 @@ This module has no hard requirements, but softly depends on:
- [[doom-package:py-isort]] requires [[https://github.com/timothycrosley/isort][isort]] to be installed: ~pip install isort~
- Python virtual environments install instructions at:
- [[https://github.com/pyenv/pyenv][pyenv]]
- [[https://github.com/astral-sh/uv][uv]]
- [[https://conda.io/en/latest/][Conda]]
- [[https://python-poetry.org/][Poetry]]
- [[https://pipenv.readthedocs.io/en/latest/][pipenv]]

View file

@ -0,0 +1,26 @@
;;; lang/python/autoload/uv.el -*- lexical-binding: t; -*-
;;;###if (modulep! +uv)
;;;###autoload
(defvar +uv--project nil)
;;;###autoload
(defun +python-uv-mode-set-auto-h ()
"Set uv-mode virtualenv from buffer-local variable."
(when (memq major-mode '(python-mode python-ts-mode))
(when (not (local-variable-p '+uv--project))
(make-local-variable '+uv--project)
(setq +uv--project (+python-uv-read-project-from-file)))
(if +uv--project
(uv-mode-set)
(uv-mode-unset))))
;;;###autoload
(defun +python-uv-read-project-from-file ()
"Read uv project root from .venv directory presence."
(when-let (root-path (projectile-locate-dominating-file default-directory ".venv"))
(let ((venv-path (expand-file-name ".venv" root-path)))
(if (file-directory-p venv-path)
root-path
(message "uv: .venv directory not found in `%s'." root-path)
nil))))

View file

@ -216,6 +216,17 @@
(add-hook 'doom-switch-buffer-hook #'+python-pyenv-mode-set-auto-h))
(use-package! uv-mode
:when (modulep! +uv)
:after python
:config
(when (executable-find "uv")
(uv-mode +1))
(add-hook! '(python-mode-local-vars-hook python-ts-mode-local-vars-hook)
#'+python-uv-mode-set-auto-h)
(add-hook 'doom-switch-buffer-hook #'+python-uv-mode-set-auto-h))
(use-package! conda
:when (modulep! +conda)
:after python

View file

@ -26,6 +26,10 @@
(unless (split-string (shell-command-to-string "pyenv versions --bare") "\n" t)
(warn! "No versions of python are available via pyenv, did you forget to install one?"))))
(when (modulep! +uv)
(unless (executable-find "uv")
(warn! "Couldn't find uv in your PATH")))
(when (modulep! +conda)
(unless (executable-find "conda")
(warn! "Couldn't find conda in your PATH")))

View file

@ -19,6 +19,8 @@
(package! pyvenv :pin "31ea715f2164dd611e7fc77b26390ef3ca93509b")
(when (modulep! +pyenv)
(package! pyenv-mode :pin "364bddb8f0c8ec022796210d8d3625a520e984b0"))
(when (modulep! +uv)
(package! uv-mode :pin "7e7f9b90832210b65823c3d58e3255cd164394b7"))
(when (modulep! +conda)
(package! conda :pin "8a1a934a2de576d4158b1b12329be4f5be931a4a"))
(when (modulep! +poetry)