diff --git a/modules/lang/python/README.org b/modules/lang/python/README.org index a0ef69f5b..ee3ede890 100644 --- a/modules/lang/python/README.org +++ b/modules/lang/python/README.org @@ -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]] diff --git a/modules/lang/python/autoload/uv.el b/modules/lang/python/autoload/uv.el new file mode 100644 index 000000000..f532cacbc --- /dev/null +++ b/modules/lang/python/autoload/uv.el @@ -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)))) diff --git a/modules/lang/python/config.el b/modules/lang/python/config.el index a0c0c0603..6a7b87426 100644 --- a/modules/lang/python/config.el +++ b/modules/lang/python/config.el @@ -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 diff --git a/modules/lang/python/doctor.el b/modules/lang/python/doctor.el index 47af20fcd..15fa9037c 100644 --- a/modules/lang/python/doctor.el +++ b/modules/lang/python/doctor.el @@ -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"))) diff --git a/modules/lang/python/packages.el b/modules/lang/python/packages.el index ffb545812..1a41d4fff 100644 --- a/modules/lang/python/packages.el +++ b/modules/lang/python/packages.el @@ -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)