reddit / emacs

Setting up Eglot for Python

velbunadur · 2022-10-26 15:43
I'd like to try using Eglot for Python coding. Can anyone advise me how to do a basic setup, preferably using use-package? Many thanks.
_rs · 2022-10-26 16:07
What have you tried so far?
scherbi · 2022-10-26 17:25
Emcs Coniguration Generator [https://emacs.amodernist.com/](https://emacs.amodernist.com/)
velbunadur · 2022-10-26 17:32
Thank you!
thetemp_ · 2022-10-26 17:47
Basic setup: 1. Install one of the supported Python [language servers](https://github.com/joaotavora/eglot#connecting-to-a-server) on your system. 2. Do one of the following: (a) Compile the latest development version of Emacs from the master branch, since it includes all the necessary package versions as built-ins. Or (b), Install the following packages from Elpa using your favorite package manager: eglot flymake eldoc xref project. 3. Open a Python file that you'd like to work on. 4. Type "M-x eglot" and press <enter>. If you want Eglot to start automatically whenever you open a python file, then add `eglot-ensure` to `python-mode-hook`. You can do that using the customize system, or (IIRC) using use-package's `:hook` keyword. I won't try to remember how it's done with use-package, but here is how it looks in regular elisp that you can copy and paste to your config: (add-hook 'python-mode-hook 'eglot-ensure) Note that the call to `add-hook` works even if called before `python-mode-hook` has been defined, which is nice.
scherbi · 2022-10-26 18:48
You're welcome!
velbunadur · 2022-10-26 19:08
That's really helpful - thanks! I've used lsp-mode before but found that a bit buggy so I've got a python language server already and I'm using Emacs 28.1 with pretty much all those packages apart from Eglot. I'll give that a go. Thanks again.
ForkInBrain · 2022-10-26 22:27
For Python specifically I feel like the hardest part is picking the language server.
thetemp_ · 2022-10-26 22:59
After reading that article about vscode and .NET yesterday, I feel like I'd rather avoid the Microsoft one(s) (pyright, and I think there's one more). That leaves pylsp and jedi-language-server. I've used each of them a little bit, but not enough to notice a difference.
velbunadur · 2022-10-27 06:50
I've just followed your instructions and using the python lsp server that comes with pip install it all seems to work perfectly. Thank you.
Dhouzy · 2022-10-27 15:41
This snippet is what I'm using to configure eglot. The thing I found to be the more difficult is to make pyls find the project virtualenv/dependancies, for that I use https://github.com/cybniv/poetry.el with the minor mode `poetry-tracking-mode`. Hopefully it will help you. (use-package eglot :ensure t :config (add-to-list 'eglot-server-programs '(python-mode . ("pylsp"))) (setq-default eglot-workspace-configuration '((:pylsp . (:configurationSources ["flake8"] :plugins (:pycodestyle (:enabled nil) :mccabe (:enabled nil) :flake8 (:enabled t)))))) :hook ((python-mode . eglot-ensure)))
velbunadur · 2022-10-27 16:22
That's very helpful - thank you!