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 Server: [pyright](https://github.com/microsoft/pyright)
  7
  8### Configuration
  9
 10The [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 precedent over `pyproject.toml` if both are present.
 11
 12For more information, see the Pyright [configuration documentation](https://microsoft.github.io/pyright/#/configuration).
 13
 14### Settings
 15
 16The [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`.
 17
 18For example, in order to:
 19- use strict type-checking level
 20- diagnose all files in the workspace instead of the only open files default
 21- provide the path to a specific python interpreter
 22
 23```json
 24{
 25  "lsp": {
 26    "pyright": {
 27      "settings": {
 28        "python.analysis": {
 29          "diagnosticMode": "workspace",
 30          "typeCheckingMode": "strict"
 31        },
 32        "python": {
 33          "pythonPath": ".venv/bin/python"
 34        }
 35      }
 36    }
 37  }
 38}
 39```
 40
 41For more information, see the Pyright [settings documentation](https://microsoft.github.io/pyright/#/settings).
 42
 43### Virtual environments
 44
 45A 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.
 46
 47By 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.
 48
 49To do this, create a JSON file called `pyrightconfig.json` at the root of your project. This file must include two keys:
 50
 51- `venvPath`: a relative path from your project directory to any directory that _contains_ one or more virtual environment directories
 52- `venv`: the name of a virtual environment directory
 53
 54For example, a common approach is to create a virtual environment directory called `.venv` at the root of your project directory with the following commands:
 55
 56```bash
 57# create a virtual environment in the .venv directory
 58python3 -m venv .venv
 59# set up the current shell to use that virtual environment
 60source .venv/bin/activate
 61```
 62
 63Having done that, you would create a `pyrightconfig.json` with the following content:
 64
 65```json
 66{
 67  "venvPath": ".",
 68  "venv": ".venv"
 69}
 70```
 71
 72If you prefer to use a `pyproject.toml` file, you can add the following section:
 73
 74```toml
 75[tool.pyright]
 76venvPath = "."
 77venv = ".venv"
 78```
 79
 80You can also configure this option directly in your `settings.json` file ([pyrights settings](#settings)), as recommended in [Configuring Your Python Environment](https://microsoft.github.io/pyright/#/import-resolution?id=configuring-your-python-environment).
 81
 82```json
 83{
 84  "lsp": {
 85    "pyright": {
 86      "settings": {
 87        "python": {
 88          "pythonPath": ".venv/bin/python"
 89        }
 90      }
 91    }
 92  }
 93}
 94```
 95
 96### Code formatting
 97
 98The Pyright language server does not provide code formatting. If you want to automatically reformat your Python code when saving, you'll need to specify an \_external_code formatter in your settings. See the [configuration](../configuring-zed.md) documentation for more information.
 99
100A common tool for formatting python code is [Black](https://black.readthedocs.io/en/stable/). If you have Black installed globally, you can use it to format Python files by adding the following to your `settings.json`:
101
102```json
103{
104  "languages": {
105    "Python": {
106      "formatter": {
107         "external": {
108          "command": "black",
109          "arguments": ["-"]
110        }
111      },
112      "format_on_save": "on"
113    }
114  }
115}
116```