python.md

  1# Python
  2
  3Python support is available natively in Zed.
  4
  5- Tree-sitter: [tree-sitter-python](https://github.com/tree-sitter/tree-sitter-python)
  6- Language Servers:
  7  - [microsoft/pyright](https://github.com/microsoft/pyright)
  8  - [python-lsp/python-lsp-server](https://github.com/python-lsp/python-lsp-server) (PyLSP)
  9
 10## Language Servers
 11
 12Zed supports multiple Python language servers some of which may require configuration to work properly.
 13
 14See: [Working with Language Servers](https://zed.dev/docs/configuring-languages#working-with-language-servers) for more information.
 15
 16## Virtual Environments in the Terminal {#terminal-detect_venv}
 17
 18Zed will detect Python virtual environments and automatically activate them in terminal if available.
 19See: [detect_venv documentation](../configuring-zed.md#terminal-detect_venv) for more.
 20
 21## PyLSP
 22
 23[python-lsp-server](https://github.com/python-lsp/python-lsp-server/), more commonly known as PyLSP, by default integrates with a number of external tools (autopep8, mccabe, pycodestyle, yapf) while others are optional and must be explicitly enabled and configured (flake8, pylint).
 24
 25See [Python Language Server Configuration](https://github.com/python-lsp/python-lsp-server/blob/develop/CONFIGURATION.md) for more.
 26
 27## PyRight
 28
 29### PyRight Configuration
 30
 31The [pyright](https://github.com/microsoft/pyright) language server offers flexible configuration options specified in a JSON-formatted text configuration. By default, the file is called `pyrightconfig.json` and is located within the root directory of your project. Pyright settings can also be specified in a `[tool.pyright]` section of a `pyproject.toml` file. A `pyrightconfig.json` file always takes precedence over `pyproject.toml` if both are present.
 32
 33For more information, see the Pyright [configuration documentation](https://microsoft.github.io/pyright/#/configuration).
 34
 35### PyRight Settings
 36
 37The [pyright](https://github.com/microsoft/pyright) language server also accepts specific LSP-related settings, not necessarily connected to a project. These can be changed in the `lsp` section of your `settings.json`.
 38
 39For example, in order to:
 40
 41- use strict type-checking level
 42- diagnose all files in the workspace instead of the only open files default
 43- provide the path to a specific Python interpreter
 44
 45```json
 46{
 47  "lsp": {
 48    "pyright": {
 49      "settings": {
 50        "python.analysis": {
 51          "diagnosticMode": "workspace",
 52          "typeCheckingMode": "strict"
 53        },
 54        "python": {
 55          "pythonPath": ".venv/bin/python"
 56        }
 57      }
 58    }
 59  }
 60}
 61```
 62
 63For more information, see the Pyright [settings documentation](https://microsoft.github.io/pyright/#/settings).
 64
 65### Pyright Virtual environments
 66
 67A Python [virtual environment](https://docs.python.org/3/tutorial/venv.html) allows you to store all of a project's dependencies, including the Python interpreter and package manager, in a single directory that's isolated from any other Python projects on your computer.
 68
 69By default, the Pyright language server will look for Python packages in the default global locations. But you can also configure Pyright to use the packages installed in a given virtual environment.
 70
 71To do this, create a JSON file called `pyrightconfig.json` at the root of your project. This file must include two keys:
 72
 73- `venvPath`: a relative path from your project directory to any directory that _contains_ one or more virtual environment directories
 74- `venv`: the name of a virtual environment directory
 75
 76For example, a common approach is to create a virtual environment directory called `.venv` at the root of your project directory with the following commands:
 77
 78```sh
 79# create a virtual environment in the .venv directory
 80python3 -m venv .venv
 81# set up the current shell to use that virtual environment
 82source .venv/bin/activate
 83```
 84
 85Having done that, you would create a `pyrightconfig.json` with the following content:
 86
 87```json
 88{
 89  "venvPath": ".",
 90  "venv": ".venv"
 91}
 92```
 93
 94If you prefer to use a `pyproject.toml` file, you can add the following section:
 95
 96```toml
 97[tool.pyright]
 98venvPath = "."
 99venv = ".venv"
100```
101
102You can also configure this option directly in your `settings.json` file ([pyright settings](#pyright-settings)), as recommended in [Configuring Your Python Environment](https://microsoft.github.io/pyright/#/import-resolution?id=configuring-your-python-environment).
103
104```json
105{
106  "lsp": {
107    "pyright": {
108      "settings": {
109        "python": {
110          "pythonPath": ".venv/bin/python"
111        }
112      }
113    }
114  }
115}
116```
117
118### Code formatting & Linting
119
120The Pyright language server does not provide code formatting or linting. If you want to detect lint errors and reformat your Python code upon saving, you'll need to set up.
121
122A common tool for formatting Python code is [Ruff](https://docs.astral.sh/ruff/). It is another tool written in Rust, an extremely fast Python linter and code formatter. It is available through the [Ruff extension](https://github.com/zed-industries/zed/tree/main/extensions/ruff/). To configure the Ruff extension to work within Zed, see the setup documentation [here](https://docs.astral.sh/ruff/editors/setup/#zed).
123
124<!--
125TBD: Expand Python Ruff docs.
126TBD: Ruff pyproject.toml, ruff.toml docs. `ruff.configuration`.
127-->