fredcamps / lsp-jedi

A LSP client for Emacs using Python Jedi Language Server
GNU General Public License v3.0
47 stars 4 forks source link

How to tell lsp-jedi to work on a venv? #17

Closed JulienPalard closed 2 years ago

JulienPalard commented 2 years ago

Related to: https://github.com/emacs-lsp/lsp-mode/issues/3390

Context: Sometimes I'm working with venvs (per project), sometimes not.

If I execute emacs from within the project venv, no issues as the PATH is correctly set, everything runs smoothly.

If I use emacsclient and no venv, no issues as I have ~/.local/bin in my PATH, everything runs smoothly.

But if I use emacsclient and a per-project-venv, I may start emacs from "outside" of the venv, in which case flycheck gets lost as it does not gets the right interpreter, not seeing the right set of installed packages, ... (while xref from within the project still works), but xref to a dependency in a venv don't).

As lsp-mode knows the project root, and venvs are typically named .venv or venv at the project root, lsp-jedi could spot them and « use them », so flycheck, xref to venv, could work with the venv.

I think it could even be done "blindly" by prefixing PROJECT_ROOT/.venv/bin/:PROJECT_ROOT/venv/bin/ to the path, there's no need to check if they actually exist, so if the user create the venv after lsp-jedi initialisation the venv gets picked.

Kind of automating what some people already do using direnv?

fredcamps commented 2 years ago

@JulienPalard create a .dir-locals.el file at your project root and include these lines of code bellow, your "{{PYTHON-EXECUTABLE-PATH}}" must be located inside your .venv/bin folder

; -*- mode: Lisp -*-
((python-mode . ((python-shell-interpreter . "{{PYTHON-EXECUTABLE-PATH}}"))))

I'll close this, because this is not a issue from the lsp-jedi, but feel free to tag me if you need help or ask something. I recommend to use pyvenv or virtualenvwrapper.el if this solution above not works

Best regards

fredcamps commented 2 years ago

@JulienPalard I've created a new release that have a better solution, update your plugin package and please check the new version of README

JulienPalard commented 2 years ago

Ooohhh thanks! I'll have to try it out!

8 days ago I started trying direnv, with emacs' direnv integration. It "works" but I often have to Mx- lsp-workspace-reload to take updated path into account.

Next week I'm offline, but I hope to try it later and tell you how it goes.