reddit / emacs

Help using lsp-mode/eglot for Python and virtualenvs

spookylukey · 2022-03-15 12:22
I’m currently using emacs-jedi for Python, and I’m trying to use and evaluate lsp-mode/eglot. This is the minimum I’m looking for: * “Jump to definition” should work, including when I jump into dependencies and keep going (which are installed into virtualenvs) * Syntax errors, undefined variables etc, should be highlighted * I should get completions/popups when writing code * I need for support for multiple Python projects, each in its own virtualenv, often with different Python versions, preferably with very little setup required. Trying this out with lsp-mode and the different options (pyright, pylsp), I’m hitting major bugs, and wondering if anyone else has actually got this working in a satisfactory way. With [python-lsp-server](https://github.com/python-lsp/python-lsp-server) I can’t figure out how to get it to see my virtualenv at all. In fact I can’t figure out how to set any configuration options (the docs are pretty cryptic and show no examples). With pyright, I can use configure it with pyrightconfig.json to get the server to see the code in my virtualenv, and “jump to definition” from the main project to code in virtualenv works. However, once in the venv, I’m prompted to import the file, which is annoying. If I do so, it seems to add to some global set of imported files, and has serious performance implications (e.g. if it happens to be part of Django, then I get a warning about watching 1000s of directories, which is unnecessary when I’m not going to be editing dependencies). If I don’t import, then “jump to definition” doesn’t work within that file. I'm having similar problems with lsp-jedi. With the emacs-jedi package (which isn't an LSP system), I am able to get virtualenvs to be recognised with use of .dir-locals.el, and with just one installation of jedi per major Python version (3.6, 3.7) etc. I had a very simple script that created .dir-locals.el from a template when I needed to start work on a new project. Can anyone share their recommendations and workflow for this kind of thing?
Extension_Try_6870 · 2022-03-15 17:06
I'm currently using projectile and pyenv-mode. There is a snippet in the pyenv-mode repo's readme which shows you how to automatically set the right virtenv on projectile-project-switch. ( You need to name your virtenv the same as you project). I'm not sure if this answers all of your problems.
spookylukey · 2022-03-15 22:15
Have you got this working with any python lsp-mode?
Extension_Try_6870 · 2022-03-15 23:03
I'm using lsp-mode with pylsp (the one you linked on your post). So basically if my project is ~/Dev/python_project1 I build a virtenv with the same name. (pyenv virtualenv 3.8 python_project1). Then inside emacs, I switch to ~/Dev/python_project1 using projectile which triggers pyenv-mode to set python_project1 virtenv (you need to add the snippet from pyenv-mode readme into your init.el). Lsp-mode starts and I have completion from packages installed inside the virtenv.
spookylukey · 2022-03-18 09:23
Thanks so much for your help. I'm not using pyenv, so I just dug around in the source to see what it was doing. In the end, I found that what I needed to set was `lsp-pylsp-plugins-jedi-environment` to the full path to the venv (e.g. `"/home/me/.virtualenvs/myproject"`) which I could do via `.dir-locals.el`, or a bit of custom code in a hook that used existed .dir-locals values to compute it.