Configuring Emacs, lsp-mode and the python language server.
Contents
The language server protocol was proposed by Microsoft as a way for different editors and development environments to share language analysis backends
This post describes how to configure Emacs, lsp-mode and the palantir python-language-server for improved code intelligence when working on Python projects. (I’m planning a companion post for Emacs, C++ and the cquery language server.)
Goal
Before starting, it is motivating to see what we are working towards.
With a correctly configured setup, Emacs will sport, amongst others, improved completion with interactive documentation, imenu navigation, documentation on hover, and really snazzy find definitions (=M-.=) and find references.
See the following screenshots for some examples:
Pre-requisites on the Python side
Install the python-language-server
into the virtual environment, or user environment, that you’re planning to use.
These days, I tend to use pipenv
:
|
|
The [all]
means that it installs all optional providers, e.g. yapf formatting.
Pre-requisites on the Emacs side
In Emacs, install the required and some optional packages using for example M-x package-install
:
lsp-mode
- the main language server protocol packagelsp-ui
- UI-related LSP extras, such as the sideline info, docs, flycheck, etc.company-lsp
- company-backend for LSP-based code completion.- [[https://github.com/bbatsov/projectile][
projectile
]] or [[https://github.com/technomancy/find-file-in-project][find-file-in-project
]] - we use a single function from here to determine the root directory of a project.
Emacs configuration
Add the following to your Emacs init.el
, and don’t forget to read the comments.
If you’re not yet using use-package
now
would be a good time to upgrade.
|
|
Putting it all together
Importantly, use pyenv
or
something similar to switch to the relevant virtualenv before opening the first
Python file.
When you open the file, the pyls
should be automatically started up, and you
can edit away with LSP-powered code intelligence.
This often gives better and more detailed results than
elpy
, probably because pyls uses a
mix of static and dynamic (introspection-based) analysis.
Furthermore, the handling of LSP servers in Emacs can be unified, giving the same consistent level of support across a whole range of programming languages.