|
| 1 | +# Python Language Server |
| 2 | + |
| 3 | +[](https://github.com/python-ls/python-ls/actions?query=workflow%3A%22Linux+tests%22) [](https://github.com/python-ls/python-ls/actions?query=workflow%3A%22Mac+tests%22) [](https://github.com/python-ls/python-ls/actions?query=workflow%3A%22Windows+tests%22) [](https://github.com/python-ls/python-ls/blob/master/LICENSE) |
| 4 | + |
| 5 | +A Python 2.7 and 3.5+ implementation of the [Language Server Protocol](https://github.com/Microsoft/language-server-protocol). |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +The base language server requires [Jedi](https://github.com/davidhalter/jedi) to provide Completions, Definitions, Hover, References, Signature Help, and Symbols: |
| 10 | + |
| 11 | +``` |
| 12 | +pip install python-language-server |
| 13 | +``` |
| 14 | + |
| 15 | +If the respective dependencies are found, the following optional providers will be enabled: |
| 16 | +- [Rope](https://github.com/python-rope/rope) for Completions and renaming |
| 17 | +- [Pyflakes](https://github.com/PyCQA/pyflakes) linter to detect various errors |
| 18 | +- [McCabe](https://github.com/PyCQA/mccabe) linter for complexity checking |
| 19 | +- [pycodestyle](https://github.com/PyCQA/pycodestyle) linter for style checking |
| 20 | +- [pydocstyle](https://github.com/PyCQA/pydocstyle) linter for docstring style checking (disabled by default) |
| 21 | +- [autopep8](https://github.com/hhatto/autopep8) for code formatting |
| 22 | +- [YAPF](https://github.com/google/yapf) for code formatting (preferred over autopep8) |
| 23 | + |
| 24 | +Optional providers can be installed using the `extras` syntax. To install [YAPF](https://github.com/google/yapf) formatting for example: |
| 25 | + |
| 26 | +``` |
| 27 | +pip install 'python-language-server[yapf]' |
| 28 | +``` |
| 29 | + |
| 30 | +All optional providers can be installed using: |
| 31 | + |
| 32 | +``` |
| 33 | +pip install 'python-language-server[all]' |
| 34 | +``` |
| 35 | + |
| 36 | +If you get an error similar to `'install_requires' must be a string or list of strings` then please upgrade setuptools before trying again. |
| 37 | + |
| 38 | +``` |
| 39 | +pip install -U setuptools |
| 40 | +``` |
| 41 | + |
| 42 | +### 3rd Party Plugins |
| 43 | + |
| 44 | +Installing these plugins will add extra functionality to the language server: |
| 45 | + |
| 46 | +- [pyls-mypy](https://github.com/tomv564/pyls-mypy) Mypy type checking for Python 3 |
| 47 | +- [pyls-isort](https://github.com/paradoxxxzero/pyls-isort) Isort import sort code formatting |
| 48 | +- [pyls-black](https://github.com/rupert/pyls-black) for code formatting using [Black](https://github.com/ambv/black) |
| 49 | +- [pyls-memestra](https://github.com/QuantStack/pyls-memestra) for detecting the use of deprecated APIs |
| 50 | + |
| 51 | +Please see the above repositories for examples on how to write plugins for the Python Language Server. Please file an issue if you require assistance writing a plugin. |
| 52 | + |
| 53 | +## Configuration |
| 54 | + |
| 55 | +Configuration is loaded from zero or more configuration sources. Currently implemented are: |
| 56 | + |
| 57 | +- pycodestyle: discovered in `~/.config/pycodestyle`, `setup.cfg`, `tox.ini` and `pycodestyle.cfg`. |
| 58 | +- flake8: discovered in `~/.config/flake8`, `setup.cfg`, `tox.ini` and `flake8.cfg` |
| 59 | + |
| 60 | +The default configuration source is pycodestyle. Change the `pyls.configurationSources` setting to `['flake8']` in order to respect flake8 configuration instead. |
| 61 | + |
| 62 | +Overall configuration is computed first from user configuration (in home directory), overridden by configuration passed in by the language client, and then overriden by configuration discovered in the workspace. |
| 63 | + |
| 64 | +To enable pydocstyle for linting docstrings add the following setting in your LSP configuration: |
| 65 | +`\` "pyls.plugins.pydocstyle.enabled": true \`` |
| 66 | + |
| 67 | +See [vscode-client/package.json](vscode-client/package.json) for the full set of supported configuration options. |
| 68 | + |
| 69 | +## Language Server Features |
| 70 | + |
| 71 | +Auto Completion: |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +Code Linting with pycodestyle and pyflakes: |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +Signature Help: |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +Go to definition: |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +Hover: |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +Find References: |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +Document Symbols: |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +Document Formatting: |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +## Development |
| 104 | + |
| 105 | +To run the test suite: |
| 106 | + |
| 107 | +``` |
| 108 | +pip install .[test] && pytest |
| 109 | +``` |
| 110 | + |
| 111 | +# Develop against VS Code |
| 112 | + |
| 113 | +The Python language server can be developed against a local instance of |
| 114 | +Visual Studio Code. |
| 115 | + |
| 116 | +Install [VSCode](https://code.visualstudio.com/download) |
| 117 | + |
| 118 | +```bash |
| 119 | +# Setup a virtual env |
| 120 | +virtualenv env |
| 121 | +. env/bin/activate |
| 122 | + |
| 123 | +# Install pyls |
| 124 | +pip install . |
| 125 | + |
| 126 | +# Install the vscode-client extension |
| 127 | +cd vscode-client |
| 128 | +yarn install |
| 129 | + |
| 130 | +# Run VSCode which is configured to use pyls |
| 131 | +# See the bottom of vscode-client/src/extension.ts for info |
| 132 | +yarn run vscode -- $PWD/../ |
| 133 | +``` |
| 134 | + |
| 135 | +Then to debug, click View -> Output and in the dropdown will be pyls. |
| 136 | +To refresh VSCode, press `Cmd + r` |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +This project is made available under the MIT License. |
0 commit comments