-
Notifications
You must be signed in to change notification settings - Fork 273
Python
There are two options for setting up a language server with Python:
- Microsoft Python Language Server (MPLS)- maintained by Microsoft
- pyls - maintained by Palantir
Install MPLS from the repo: https://github.com/Microsoft/python-language-server
If you are using Arch, there are two AUR packages that can be found here:
Add the following line to your .vimrc
:
let g:LanguageClient_serverCommands = {
\ 'python' : ['dotnet', 'exec', '<path-to-mpls-installation>/Microsoft.Python.LanguageServer.dll'],
\ }
MPLS needs some additional settings to work with LanguageClient so you need to modify your settings.json
. By default, LanguageClient looks in your working directory for the .vim/settings.json
so that configuration can be done on a per-project basis. To override this and have a global settings file, set the g:LanguageClient_settingsPath
variable in your .vimrc
. We will assume for the rest of this document that you set g:LanguageClient_settingsPath = '~/.vim/LanguageClient_settings.json'
if you choose to use the global method.
While having a global file is much simpler, this leads to an issue with the typical environment based workflow in Python. Specifically, the settings for MPLS require you to specify a path to the python executable. If you use a global settings.json
and set a global python executable, for example /usr/bin/python
, you don't need to create a settings file for every new project directory. However, if you now enter a virtual environment and install a library, say numpy
, which you don't have installed outside of your virtual environment, the language server will give you an unresolved import
error. Further, completion will not be available for code from that library.
The solution is to use the default per-project .vim/settings.json
file and set manually set the InterpreterPath
key to the virtual environment specific python executable. There is currently no way using LanguageClient (that I'm aware of) to work around this (i.e. something like set a global settings file, check if you are in a virtual environment and accordingly update your settings file).
Regardless of your choice of global or per-project basis, add the following to your settings file:
{
"enabled": true,
"initializationOptions": {
"displayOptions": {
"preferredFormat": "plaintext",
"trimDocumentationLines": true,
"maxDocumentationLineLength": 0,
"trimDocumentationText": true,
"maxDocumentationTextLength": 0
},
"interpreter": {
"properties": {
"InterpreterPath": "<path-to-python-executable>",
"UseDefaultDatabase": true,
"Version": "<python-version>"
}
}
}
}
The entirety of the instructions here are obtained from this issue: https://github.com/autozimu/LanguageClient-neovim/issues/633
Install pyls from the repo: https://github.com/palantir/python-language-server.
You can also install directly via pip:
pip install pyls
If you are using Arch, there is a Community package that can be found here:
pyls works by combining the functionality of several other python libraries. You will need to install those as well to use it fully. See the README in the repo to find out which python libraries you need. pyls works by combining the functionality of several other python libraries. You will need to install those as well to use it fully. See the README in the repo to find out which python libraries you need.
Add the following line to your .vimrc
:
let g:LanguageClient_serverCommands = {
\ 'python' : ['pyls']
\ }