2018-12-05
This post is now a bit outdated an updated post is here.
In my Emacs rabbit hole I mentioned in my previous post, I decided to work on improving my Python development workflow. I recently found the Eglot package for running a LSP in Emacs.
The most vanilla setup for Eglot is just M-x eglot
in a buffer
editing a python file. This works wonderfully if the executable for
the Python Language
Server (pyls
)
is found. This works because Eglot defines a list of server programs
by default. See this list with M-: eglot-server-programs
I have a few python virtual/Anaconda environments I like to work with.
This is what .dir-locals.el
is for:
where /path/to/env
is the path to a virtual environment or Anaconda
environment (that of course has python-language-server
installed). I
also define the path to my Python executable for Emacs' builtin
python.el
. By default, company-backends
includes company-capf
for completion-at-point
, but I want to make sure that's what is used
because Eglot provides completion-at-point
. Eglot also has pyls
as
a python-mode
entry by default, but not to the virtual environment I
want to use; this is why I manually define the list of server
programs.
When I open a buffer in the project I want to work in, I just call
M-x eglot
and I'm up and running.
If I'm not editing in a project that has an associated virtual environment, I rely on some "sensible defaults" in my Emacs init file:
This way I have a default pyls
executable from my base
Anaconda
environment (which is potentially different on different machines). I
then have a couple of functions to handle default Eglot python
environments, where I:
use-package
install Eglot if necessary.company-capf
is at the front of company-backends
.base
Anaconda pyls
to the front of the eglot-server-programs
list.
I just bring company-capf
to the front of the company-backends
list, and add my desired Anaconda based pyls
to front of the
eglot-server-programs
list.