sourcegraph / python-langserver

Language server which talks LSP via JSONRPC for Python.
MIT License
102 stars 12 forks source link

Python language server


NOTE: This project is no longer being maintained or in active development. See the Sourcegraph fork of Microsoft's Python language server.


Build Status

This is a language server for Python that adheres to the Language Server Protocol (LSP). It uses Jedi to perform source code analysis. Python versions 2.x and 3.x are supported.

Automatic Dependency Installation

Configuring pip

python-langserver uses pip to fetch dependencies. To configure the behavior of pip, you can supply the pipArgs initalizationOption field inside the initalize request parameters. pipArgs specifies a list of arguments to add to the invocation of pip, for example:

InitializeParams {
    //...
    "initializationOptions": {
        "pipArgs": [
            "--index-url=https://python.example.com",
            "--extra-index-url=https://pypi.python.org/simple"
        ]
        // ...
    }
}

This will tell pip to use https://python.example.com as its base package index and https://pypi.python.org/simple as an extra package index, as described in the pip documentation.

Note, when using this language server with Sourcegraph - you can set the initializationOptions in your site configuration:


{
    // ...
    "langservers": [
        {
            "language": "python",
            "initializationOptions": {
                "pipArgs": [
                    "--index-url=https://python.example.com",
                    "--extra-index-url=https://pypi.python.org/simple"
                ]
            }
        }
    ]
    // ...
}

Inference of Package Names

The language server will not run setup.py or pip install. When it encounters an import, it tries to infer the package name and run pip download. (This also avoids running the downloaded package's setup.py.) This is expected to work as long as the name of the package on PyPI (or your private package index) is the same as the name that's imported in the source code.

Development

Getting started

You'll need Python version 3.6 or greater or pyenv installed. You will also need pipenv installed:

  1. pipenv install
  2. pipenv run python python-langserver.py --mode=tcp --addr=2087

To try it in Visual Studio Code, install the vscode-client extension and then open up a .py file.

Tests

Run make test.

The tests require pytest. Note that some tests may fail if you're using virtualenv instead of the system Python.