Use the project virtualenv for the Python IDE

This commit is contained in:
Ohad Livne 2025-06-04 22:44:31 +03:00
parent 46cdf13182
commit 2a90ef5c0d
Signed by: libohad-dev
GPG key ID: 34FDC68B51191A4D
3 changed files with 29 additions and 3 deletions

View file

@ -126,8 +126,29 @@
("C-c C-p" . flymake-goto-prev-error)
("C-c C-l" . flymake-show-diagnostics-buffer)))
(use-package pyvenv)
(defun find-venv ()
"Try to find a .venv/ directory in the current project"
(require 'f)
(let ((basedir (f-traverse-upwards
(lambda (path)
(let ((venv-path (f-expand ".venv" path)))
(f-exists? venv-path))))))
(when basedir (f-expand ".venv" basedir))))
(defun load-python-env ()
"Set up the Python IDE in the current project"
(interactive)
(let ((venv-dir (find-venv)))
(if venv-dir
(progn
(pyvenv-activate venv-dir)
(lsp))
(pyvenv-deactivate))))
(use-package python
:bind (:map python-mode-map
("C-c C-p" . nil)
("C-c C-l" . nil))
:hook ((python-mode . lsp)))
:hook ((python-mode . load-python-env)))

View file

@ -9,8 +9,7 @@ htop \
ibus-mozc \
keepassxc \
mpv \
python3-flake8 \
python3-pylsp \
python3-venv
sway \
swayidle \
swaylock \

6
.local/bin/make-venv Executable file
View file

@ -0,0 +1,6 @@
#! /usr/bin/sh
python3 -m venv .venv
. .venv/bin/activate
pip install 'python-lsp-server[all]'
pip install -e .