- Saved searches
- Use saved searches to filter your results more quickly
- License
- microsoft/python-language-server
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Python LSP Server
- Windows and Linux installation
- 3rd Party Plugins
- Configuration
- LSP Server Features
- Development
- License
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Microsoft Language Server for Python
License
microsoft/python-language-server
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Microsoft Python Language Server
Microsoft Python Language Server implements the Language Server Protocol.
Feel free to file issues or ask questions on our issue tracker, and we welcome code contributions.
Troubleshooting and known issues
Linting options (diagnostics)
The language server implements diagnostics (or linting), which runs on user code. The following diagnostics are supported:
Code | Description |
---|---|
inherit-non-class | Attempted to inherit something that is not a class. |
too-many-function-arguments | Too many arguments have been provided to a function call. |
too-many-positional-arguments-before-star | Too many arguments have been provided before a starred argument. |
no-cls-argument | First parameter in a class method must be cls |
no-method-argument | Method has no arguments |
no-self-argument | First parameter in a method must be self |
parameter-already-specified | A argument with this name has already been specified. |
parameter-missing | A required positional argument is missing. |
positional-argument-after-keyword | A positional argument has been provided after a keyword argument. |
positional-only-named | A positional-only argument (3.8+) has been named in a function call. |
return-in-init | Encountered an explicit return in __init__ function. |
typing-generic-arguments | An error occurred while constructing Generic . |
typing-newtype-arguments | An error occurred while constructing NewType . |
typing-typevar-arguments | An error occurred while constructing TypeVar . |
unknown-parameter-name | The keyword argument name provided is unknown. |
unresolved-import | An import cannot be resolved, and may be missing. |
undefined-variable | A variable has been used that has not yet been defined. |
variable-not-defined-globally | A variable is not defined in the global scope. |
variable-not-defined-nonlocal | A variable is not defined in non-local scopes. |
Linting can be controlled via the user configuration. In VS Code, this is settings.json , but other clients would send this via workspace/didChangeConfiguration .
If python.linting.enabled is set to false in the user configuration, then no diagnostics will be collected other than syntax errors and unresolved imports.
To control the visibility and severity of the diagnotics, there are a number of lists that can be set in the user configuration which make use of each diagnostic’s error code.
Setting | Description |
---|---|
python.analysis.errors | Diagnostics which should be shown as errors. |
python.analysis.warnings | Diagnostics which should be shown as warnings. |
python.analysis.information | Diagnostics which should be shown as informational. |
python.analysis.disabled | Diagnotics which should not be shown at all. |
An example of a user configuration which sets these options:
< "python.analysis.errors": ["undefined-variable"], "python.analysis.warnings": ["unknown-parameter-name"], "python.analysis.information": ["unresolved-import"], "python.analysis.disabled": ["too-many-function-arguments", "parameter-missing"], >
Linting can also be controlled on an individual line basis with a generalized #noqa . Lines with #noqa will have their diagnostic output suppressed.
from python import language_server # noqa will suppress the linting message for this line
During analysis language server produces Python code from compiled modules and builtins which is similar to Python module stubs. It may also produce database files holding module analysis for faster retrieval later. Cache location is at
«%LOCALAPPDATA%\Microsoft\Python Language Server» (which is Environment.SpecialFolder.LocalApplicationData ). Typically «C:\Users\\%USER_NAME%\AppData\Local\Microsoft\Python Language Server»
«$XDG_CACHE_HOME/Microsoft/Python Language Server» , or if XDG_CACHE_HOME is not set, «$HOME/.cache/Microsoft/Python Language Server»
«$HOME/Library/Caches/Microsoft/Python Language Server»
Python LSP Server
The base language server requires Jedi to provide Completions, Definitions, Hover, References, Signature Help, and Symbols:
pip install python-lsp-server
This will expose the command pylsp on your PATH. Confirm that installation succeeded by running pylsp —help .
If the respective dependencies are found, the following optional providers will be enabled:
- Rope for Completions and renaming
- Pyflakes linter to detect various errors
- McCabe linter for complexity checking
- pycodestyle linter for style checking
- pydocstyle linter for docstring style checking (disabled by default)
- autopep8 for code formatting
- YAPF for code formatting (preferred over autopep8)
- flake8 for error checking (disabled by default)
- pylint for code linting (disabled by default)
Optional providers can be installed using the extras syntax. To install YAPF formatting for example:
pip install "python-lsp-server[yapf]"
All optional providers can be installed using:
pip install "python-lsp-server[all]"
If you get an error similar to ‘install_requires’ must be a string or list of strings then please upgrade setuptools before trying again.
Windows and Linux installation
If you use Anaconda/Miniconda, you can install python-lsp-server using this conda command
conda install -c conda-forge python-lsp-server
Python-lsp-server is available in the repos of every major Linux distribution, and it is usually called python-lsp-server or python3-pylsp .
For example, here is how to install it in Debian and Debian-based distributions (E.g. Ubuntu, Pop!_OS, Linux Mint)
sudo apt-get install python3-pylsp
sudo dnf install python-lsp-server
sudo pacman -S python-lsp-server
Only on Alpine Linux the package is named differently. You can install it there by typing this command in your terminal:
3rd Party Plugins
Installing these plugins will add extra functionality to the language server:
- pylsp-mypy: MyPy type checking for Python >=3.7.
- pyls-isort: code formatting using isort (automatic import sorting).
- python-lsp-black: code formatting using Black.
- pyls-memestra: detecting the use of deprecated APIs.
- pylsp-rope: Extended refactoring capabilities using Rope.
- python-lsp-ruff: Extensive and fast linting using ruff.
Please see the above repositories for examples on how to write plugins for the Python LSP Server.
cookiecutter-pylsp-plugin is a cookiecutter template for setting up a basic plugin project for python-lsp-server. It documents all the essentials you need to know to kick start your own plugin project.
Please file an issue if you require assistance writing a plugin.
Configuration
Like all language servers, configuration can be passed from the client that talks to this server (i.e. your editor/IDE or other tool that has the same purpose). The details of how this is done depend on the editor or plugin that you are using to communicate with python-lsp-server . The configuration options available at that level are documented in CONFIGURATION.md .
python-lsp-server depends on other tools, like flake8 and pycodestyle. These tools can be configured via settings passed from the client (as above), or alternatively from other configuration sources. The following sources are available:
- pycodestyle : discovered in ~/.config/pycodestyle , setup.cfg , tox.ini and pycodestyle.cfg .
- flake8 : discovered in ~/.config/flake8 , .flake8 , setup.cfg and tox.ini
The default configuration sources are pycodestyle and pyflakes . If you would like to use flake8 , you will need to:
- Disable pycodestyle , mccabe , and pyflakes , by setting their corresponding enabled configurations, e.g. pylsp.plugins.pycodestyle.enabled , to false . This will prevent duplicate linting messages as flake8 includes these tools.
- Set pylsp.plugins.flake8.enabled to true .
- Change the pylsp.configurationSources setting (in the value passed in from your client) to [‘flake8’] in order to use the flake8 configuration instead.
The configuration options available in these config files ( setup.cfg etc) are documented in the relevant tools:
Overall configuration is computed first from user configuration (in home directory), overridden by configuration passed in by the language client, and then overridden by configuration discovered in the workspace.
As an example, to change the list of errors that pycodestyle will ignore, assuming you are using the pycodestyle configuration source (the default), you can:
- Add the following to your ~/.config/pycodestyle:
[pycodestyle] ignore = E226,E302,E41
Python LSP Server can communicate over WebSockets when configured as follows:
The following libraries are required for Web Sockets support:
You can install this dependency with command below:
pip install 'python-lsp-server[websockets]'
LSP Server Features
- Auto Completion
- Autoimport
- Code Linting
- Code actions
- Signature Help
- Go to definition
- Hover
- Find References
- Document Symbols
- Document Formatting
- Code folding
- Multiple workspaces
Development
After adding configuration options to schema.json , refresh the CONFIGURATION.md file with
python scripts/jsonschema2md.py pylsp/config/schema.json CONFIGURATION.md
License
This project is made available under the MIT License.