reddit / emacs
Python IDE using Eglot?
International-Can107 · 2022-05-18 16:57
Anyone know of a modern tutorial for beginners teaching how to setup Emacs as a Python IDE, [much like this one](https://realpython.com/emacs-the-best-python-editor/), using Eglot instead of Elpy?
NoisyFrequency · 2022-05-18 17:26
Do you use `use-package`? and what version of Emacs do you have?
International-Can107 · 2022-05-18 17:53
Yes, I'm using use-package for loading hcl-mode and trying new things like corfu. I'm using emacs 27.2, but I can install emacs 28.1 if needed.
NoisyFrequency · 2022-05-18 18:00
Emacs `28.1` has a lot of cool stuff (See /u/mickeyp's [post here](https://www.masteringemacs.org/article/whats-new-in-emacs-28-1) about it). So if you have the opportunity, I think it'd be great to have.
Also, how do you manage your Python environments? This is important to know which packages could help you.
International-Can107 · 2022-05-18 18:44
I'm learning python currently, and at this point I can only program simple scripts for system administration. Never got to use venv.
I'm trying to get emacs to behave like vscode (intellisense auto completion and language diagnostic).
NoisyFrequency · 2022-05-18 19:01
Alright, I'll show you something you can use.
NoisyFrequency · 2022-05-18 19:07
Ok OP, I've created (again) a Python setup with Emacs. This should provide a proper Python setup hopefully.
[See the setup here](https://gist.github.com/Nathan-Furnal/b327f14e861f009c014af36c1790ec49).
If you have any question, you can ask them here, I've added lots of comments for clarity.
My [own config](https://github.com/Nathan-Furnal/dotemacs) does not use `eglot` but `lsp-mode` (use the one you prefer, eglot is a good choice nonetheless). You can pick stuff you like from there as well.
International-Can107 · 2022-05-18 19:18
THANKS A LOT!!!
I'm installing emacs 28.1 right now to try this!
NoisyFrequency · 2022-05-18 19:21
No problem! There are some things that might now work (I'm thinking about the fonts that I marked as optional) but you can always get rid of that.
sivadd_ · 2022-05-18 20:08
I use `eglot` with `pyright` and it works very well. I lean on `pyvenv` to activate whatever environment I'd like to use for a given project. Running `M-x eglot` automatically finds the `pyright-langserver` executable.
International-Can107 · 2022-05-18 20:29
I installed pyright ...
`sudo npm install -g pyright`
and It seams to be working fine. I'm getting this warning, though:
`#[eglot] (warning) Server tried to register unsupported capability \workspace/didChangeWorkspaceFolders'`
Maybe I need a project.el hook somewhere? I'll try to debug this tomorrow.
NoisyFrequency · 2022-05-18 21:02
You don't actually need to install pyright from npm. You can install straight from eglot.
For the project you need to have a configuration file, otherwise it won't know what the workspace is or where the root of the project is.
ank_itsharma · 2022-07-28 17:06
What does your config look like for this setup?
sivadd_ · 2022-07-28 17:56
(use-package pyvenv
:ensure t
:init
(setenv "WORKON_HOME" "~/.pyenv/versions"))
(use-package eglot
:ensure t
:commands eglot
:config
(add-to-list 'eglot-server-programs
`(python-mode
. ,(eglot-alternatives '("pylsp"
"jedi-language-server"
("pyright-langserver" "--stdio")))))
(setq eglot-autoshutdown t))
I use `M-x pyvenv-workon` to select a virtual env and then `M-x eglot`
steve_b · 2022-10-07 14:59
Thanks for your init.el. I have my own that is quite developed, but I've never really bothered to put together a decent setup for Python. I'm using emacs v26.1; I also don't use use-package.
However, when I drop that init.el into a fresh .emacs.d, I get startup errors. The first time it goes through an elpa-download cycle, but after that, starting emacs just generates an error saying that use-package is not found (despite being in the elpa directory). Elsewhere on stackoverflow, I found this startup sequence that works for use-package:
`;; This is the package system for Emacs. We have to load it to install \`use-package'.(require 'package)(add-to-list 'package-archives'("melpa" . "[https://melpa.org/packages/](https://melpa.org/packages/)") t)(package-initialize);; Bootstrap 'use-package'(eval-after-load 'gnutls'(add-to-list 'gnutls-trustfiles "/etc/ssl/cert.pem"))(unless (package-installed-p 'use-package)(package-refresh-contents)(package-install 'use-package))(eval-when-compile(require 'use-package))(require 'bind-key)(setq use-package-always-ensure t)\`
After that, I'm getting errors as soon as I try to open a python file, with it complaining:
`?\205\0\307\310\311 \")\207" [#<buffer download_weight.py> (#0) eglot--managed-mode remove-hook post-command-hook nil buffer-live-p apply eglot--connect eglot--guess-contact] 4]): (error "None of ’pylsp, pyls, pyright-langserver, jedi-language-server’ are valid executables")`
How do I install pyright? Uncommenting the lsp-pyright section doesn't work, and I don't see pyright as an apt package, nor does the pyright github page talk about installation. Do I just clone the repo, and build/install it myself?
If I just uncomment the lsp-pyright section (and comment out eglot), I get a blue message in the minibuffer that says:
`Unable to find installed server supporting this file. The following servers could be installed automatically:`
with nothing listed after.
EDIT:
Okay, apparently pyright is distributed via snap:
`sudo snap install pyright --classic`
NoisyFrequency · 2022-10-07 16:08
When you install `lsp-pyright` with `MELPA` it should download on its own with `npm` (a package manager for Javascript and Typescript).
steve_b · 2022-10-07 16:15
I tried that, both with M-x packages-list-packages and using the (package-use) block in your setup. Neither actually installed the tool.
EDIT: By the way, once I installed with snap, it all works very well. Much nicer than the rather dodgy jedi-based scheme I had before.
steve_b · 2022-10-07 16:35
Also, I think there's an error in your init. This line:
`:hook (python-mode-hook . blacken-mode))`
should be:
`:hook (python-mode . blacken-mode))`
NoisyFrequency · 2022-10-07 16:36
You should use `python-mode` in your configuration! Mine uses `python-mode-hook` because I disabled the automatic `-hook` insertion at the end.
NoisyFrequency · 2022-10-07 16:36
Good to know =)
ank_itsharma · 2022-10-08 19:11
>For the project you need to have a configuration file, otherwise it won't know what the workspace is or where the root of the project is.
what is the format for the config file?
NoisyFrequency · 2022-10-08 20:58
Usually LSP will expect a `.json` file for the config. You'll have to read the docs for pyright.