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### Virtual environments
15
16A 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.
17
18By 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.
19
20To do this, create a JSON file called `pyrightconfig.json` at the root of your project. This file must include two keys:
21
22- `venvPath`: a relative path from your project directory to any directory that _contains_ one or more virtual environment directories
23- `venv`: the name of a virtual environment directory
24
25For example, a common approach is to create a virtual environment directory called `.venv` at the root of your project directory with the following commands:
26
27```bash
28# create a virtual environment in the .venv directory
29python3 -m venv .venv
30# set up the current shell to use that virtual environment
31source .venv/bin/activate
32```
33
34Having done that, you would create a `pyrightconfig.json` with the following content:
35
36```json
37{
38 "venvPath": ".",
39 "venv": ".venv"
40}
41```
42
43If you prefer to use a `pyproject.toml` file, you can add the following section:
44
45```toml
46[tool.pyright]
47venvPath = "."
48venv = ".venv"
49```
50
51### Code formatting
52
53The 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.
54
55A 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`:
56
57```json
58{
59 "languages": {
60 "Python": {
61 "formatter": {
62 "external": {
63 "command": "black",
64 "arguments": ["-"]
65 }
66 },
67 "format_on_save": "on"
68 }
69 }
70}
71```