reddit / emacs
Setup Eglot in Emacs29 to recognize Python virtualenv
kentrose · 2022-12-17 23:24
In Emacs29 I've moved from LSP to Eglot, Treesitter to tsetter, and Company to Corfu - and all is working nicely for Typescript development.
I'm trying to setup for Python development, and have spent an entire day trying to get it to recognize a virtualenvironment. This is working in Elpy, but I'd really prefer to use eglot for Python.
My python config is...
(use-package python-mode :defer
:mode
("\\.py\\'" . python-ts-mode)
:interpreter "ipython"
:custom
(truncate-lines t))
(use-package pyvenv
:ensure t
:init
(setenv "WORKON_HOME" "~/.pyenv/versions"))
And I have verified that `eglot-current-server` is "\~/.cache/fnm\_multishells/84695\_1671319057550/bin/pyright-langserver --stdio". I test for whether the virtualenv is active by whether `pandas_datareader` is found. It's not, though I know it's in the virtualenv (testing in ipython's cli).
Anyone successful loading Python virtualenvs in eglot in Emacs29?
doolio_ · 2022-12-17 23:50
A [similar question ](https://www.reddit.com/r/emacs/comments/zbkbyy/is_there_a_way_to_have_eglot_pickup_the_virtual) was asked recently and there I provided my config.
kentrose · 2022-12-18 02:55
I did see that post (thanks for your input there!), but I'm still having issues. My setup is a bit different -- not using `asdf` or `direnv`, using `pyenv` rather than `poetry`, but have been using `exec-path-from-shell`.
I've confirmed that `exec-path` matches the $PATH when I'm in a shell, and that the correct virtualenv is the version of Python that is first on the $PATH -- `$HOME/.pyenv/shims/python` since I'm using pyenv for managing Python versions.
The problem is that eglot doesn't seem to be picking up this virtualenv from the path, and I'm not sure from your configuration where the `exec-path` for eglot is set.
kentrose · 2022-12-18 03:00
BTW: One other bit of information to add. I'm able to execute `pyvenv-workon` and select the correct virtualenv, but it still doesn't seem to be respected by eglot - packages that ARE in the virtualenv (like `pandas_datareader` are still not found).
kentrose · 2022-12-18 04:45
OK, solved it ... or at least, have a solution.
I was running the pyright lsp server (nodejs based), which had been installed globally via `npm install -g pyright`. I tried install the python-lsp-server (pylsp) in the virtualenv via `pip install python-lsp-server`, and now eglot works just fine.
I have no idea if this is an issue with pyright and eglot, but this combination seems to work just fine.
Falcomomo · 2022-12-18 08:53
Not directly able to help, but there is a Discord server for the System Crafters Youtube channel relating to Emacs and the people there are very helpful. There are a lot of eglot users there too, so Google for it and have a look.
I think the python-lsp-server is not very fast compared with pyright
doolio_ · 2022-12-18 09:52
Is your project considered a project from project.el's point of view i.e. have you put it under version control. There is a `project-list-file` where your project should be listed if not already. It should be added automatically but I did find that was not always the case for me. There is a project command `project-remember-projects-under` which allows you to add all projects under a directory. It will search within subdirectories too. I believe once project.el knows about your project, Eglot should know about it too.
doolio_ · 2022-12-18 12:33
Could that be related to a discussion in the other post I linked to about having the additional pandas stubs package installed? It seems the pandas project do not provide the stubs by default.
kentrose · 2022-12-19 02:02
Interesting! It wasn't there (I've now added it via `project-remember-projects-under`), but I use `projectile` rather than `project.el`, and eglot claims to (and seems to) work just fine with `projectile`. At any rate, it was working before adding the project to `project-list-file`, and continues to work now - so I'm not sure what effect that will have. But thanks.
kentrose · 2022-12-19 02:11
It's finding those packages just fine now that I switched to `pylsp`.
BTW: since `pylsp` is python based, it also identifies known issues with `pandas_datareader` ([https://mypy.readthedocs.io/en/stable/running\_mypy.html#missing-imports](https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports).) Note that this is NOT a problem - eglot functionality is working great with `pylsp` \- even for the `pandas_datareader` imports.
kentrose · 2022-12-19 02:13
Thanks Falcomomo - I'll look for that Discord channel!
FYI: I haven't noticed any issues with `pylsp` speed - at least for my workflow.
kentrose · 2022-12-19 04:41
Going to close this out as solved, and record my setup for future reference.
First, I'm on Mac and using `pyenv` to manage python versions and virtual environments. Not sure if that matters to the following, but mentioning to provide best guidance to others.
Second, I'm using emacs-plus@29 installed via homebrew.
Third, I'm using `tsitter` in emacs29. I have [https://github.com/casouri/tree-sitter-module](https://github.com/casouri/tree-sitter-module) installed as a sub-module in `~/.config/emacs`, and have a symlink called `tree-sitter` to `./tree-sitter-module/dist` (built by `./tree-sitter-module/batch.sh`) . I'm mentioning the tsitter config here since I'm using `python-ts-mode` which is tree-sitter aware.
Fourth, in the virtualenv for my project, I've executed: `pip install "python-lsp-server[all]"`
Fifth, I've installed `exec-path-from-shell` (see below)
Finally, the relevant emacs config for python is as follows:
(use-package python-mode :defer
:mode ("\\.py\\'" . python-ts-mode)
:interpreter "ipython"
:custom (truncate-lines t)
:init (exec-path-from-shell-initialize))
Good luck!