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.)
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
python-language-server into the virtual environment, or user environment, that you’re planning to use.
These days, I tend to use
[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
lsp-mode- the main language server protocol package
lsp-ui- UI-related LSP extras, such as the sideline info, docs, flycheck, etc.
company-lsp- company-backend for LSP-based code completion.
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.
Add the following to your Emacs
init.el, and don’t forget to read the comments.
If you’re not yet using
would be a good time to upgrade.
Putting it all together
something similar to switch to the relevant virtualenv before opening the first
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.