I really enjoy my minimal Emacs
configuration with pyvenv,
eglot and pyright language server when developing
Python programs. The usual workflow for me is:
- activate
pyvenvenvironmentM-x pyvenv-activateM-x pyvenv-mode(if I want the virtual env to be shown in the mode line, the previous command enables the venv anyway)
- or use
poetry-venv-activateif the project uses poetry for package management and emacs has poetry.el installed. Alternatively, one can usepyvenvand point it to the poetry env on the system M-x eglotand pickpyright-languageserver(it should be installed in the environment)
Nevertheless, there are times that I don't want the language server to be active or if I don't want to install it in a remote server to which I am connected over SSH and tramp. For this cases I use the following workflow to be able to find definitions or references of an object and run linting:
M-x project-compilefind . -name '*.py' | etags -to populate tags using GNU etags- use
M-x xref-find-definitions(or its keybinding which isg din Emacs evil) orM-x xref-find-referencesand use the TAGS file created in the root of the project - for linting and errors I do
M-x project-compilefollowed bypyright .1 orpylint *and useprevious-errorandnext-errorkeybinds to jump between errors
This provides almost everything I would need for simple and lightweight Python development environment.
pylint *
and focus on click or using next-errorFootnotes:
for pyright errors to show correctly in the emacs compilation mode, the following needs to be added the Emacs configurations:
↩︎(push 'pyright compilation-error-regexp-alist) (push '(pyright "^\\ +\\(.+\\):\\([0-9]+\\):\\([0-9]+\\).+$" 1 2 3) compilation-error-regexp-alist-alist)