# How to configure VS Code for a uv project

{{< callout type="warning" >}}
These steps assume a Python project managed with [uv](https://pydevtools.com/handbook/reference/uv.md). If you haven't created a project yet, see the [project creation tutorial](https://pydevtools.com/handbook/tutorial/create-your-first-python-project.md).
{{< /callout >}}

VS Code needs three things to work well with a [uv](https://pydevtools.com/handbook/reference/uv.md) project: the right Python {{< term "interpreter" >}}, a formatter and linter, and a test runner.

## Install the required extensions

Install these extensions from the VS Code marketplace:

- [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) (ms-python.python)
- [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) (charliermarsh.ruff)

The Python extension handles interpreter selection and test discovery. It also installs [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) (the language server behind IntelliSense and the `python.analysis.*` settings) and the [Python Debugger extension](https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy) (debugpy) automatically. The [Ruff](https://pydevtools.com/handbook/reference/ruff.md) extension provides linting and formatting.

## Select the Python interpreter

uv creates a {{< term "virtual-environment" "virtual environment" >}} in `.venv/` at the root of your project. The Python extension detects `.venv` directories automatically, so in most cases the interpreter is selected without any manual configuration.

Run `uv sync` to ensure the virtual environment exists. VS Code should pick up the `.venv` interpreter within a few seconds. If it doesn't, open the Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`) and choose **Python: Select Interpreter**, then pick the interpreter inside `.venv`.

> [!TIP]
> If VS Code shows import errors or unresolved modules after adding a dependency with `uv add`, run `uv sync` and then reload the VS Code window (`Ctrl+Shift+P` → **Developer: Reload Window**). The Python language server needs to re-index the environment.

## Configure Ruff for formatting and linting

The Ruff extension replaces both a formatter (like Black) and a linter (like flake8). First add Ruff to the project so the editor and the command line run the same version:

```bash
uv add --dev ruff
```

The extension's default `importStrategy: "fromEnvironment"` checks the active interpreter's environment for a Ruff executable before falling back to its bundled copy. With Ruff installed by uv, the extension uses `.venv/bin/ruff` (`.venv\Scripts\ruff.exe` on Windows), so the editor and terminal never drift to different versions.

Then add these settings to `.vscode/settings.json`:

```json
{
    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.fixAll.ruff": "explicit",
            "source.organizeImports.ruff": "explicit"
        }
    }
}
```

The `.ruff` suffix on `source.fixAll` and `source.organizeImports` scopes each save action to this extension, so neither one triggers another formatter you have installed. The extension reads your project's `pyproject.toml`, `ruff.toml`, or `.ruff.toml` automatically, so linting rules stay consistent between the editor and the command line.

> [!NOTE]
> If you have the older Black Formatter or isort extensions installed, disable them. The Ruff extension handles both tasks.

## Set up pytest

The Python extension can discover and run [pytest](https://pydevtools.com/handbook/reference/pytest.md) tests from the VS Code sidebar. Add pytest to the project first so the extension finds it in the `.venv`:

```bash
uv add --dev pytest
```

Then add these settings:

```json
{
    "python.testing.pytestEnabled": true,
    "python.testing.unittestEnabled": false,
    "python.testing.pytestArgs": ["tests"]
}
```

Save the file, open the Testing sidebar (beaker icon), and click **Refresh Tests**. Your test files appear as a tree, each test with a run icon beside it.

## Combine the settings in one file

Here is a `.vscode/settings.json` that combines the Ruff and pytest configurations:

```json
{
    "python.testing.pytestEnabled": true,
    "python.testing.unittestEnabled": false,
    "python.testing.pytestArgs": ["tests"],
    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.fixAll.ruff": "explicit",
            "source.organizeImports.ruff": "explicit"
        }
    }
}
```

Commit this file to version control so everyone on the team gets the same formatter and test settings. The interpreter path stays out of it because VS Code auto-detects the `.venv` directory that uv creates.

## Run a file with the play button

The play button (**Run Python File**) runs your file with the selected interpreter directly. It does not wrap the command in `uv run`, so it bypasses uv's dependency resolution for [inline script metadata](https://packaging.python.org/en/latest/specifications/inline-script-metadata/). Run standalone scripts that declare their own dependencies from the integrated terminal with `uv run script.py` instead. For project code already installed in `.venv` by `uv sync`, the play button works fine.

## Run Jupyter notebooks

To use Jupyter notebooks in VS Code with a uv project, add `ipykernel` as a {{< term "dev-dependency" "development dependency" >}}:

```bash
uv add --dev ipykernel
```

Open a `.ipynb` file and VS Code will prompt you to select a kernel. Choose the Python interpreter from your `.venv` directory. If no kernel appears, run `uv sync` first to ensure `ipykernel` is installed in the environment.

For more details, see [How to run a Jupyter notebook with uv](https://pydevtools.com/handbook/how-to/jupyter-notebook-with-uv.md).

## Add type checking with ty

[ty](https://pydevtools.com/handbook/reference/ty.md) is Astral's type checker for Python, built by the same team behind uv and Ruff. The [ty VS Code extension](https://marketplace.visualstudio.com/items?itemName=astral-sh.ty) (`astral-sh.ty`) provides type error diagnostics directly in the editor.

Add ty to the project so the editor and the command line agree:

```bash
uv add --dev ty
```

Like Ruff, the extension defaults to `ty.importStrategy: "fromEnvironment"`, so it uses the ty in `.venv` and falls back to its bundled copy only when the environment has none.

> [!NOTE]
> ty is in beta. Expect rough edges compared to mature type checkers like [mypy](https://pydevtools.com/handbook/reference/mypy.md) or [pyright](https://pydevtools.com/handbook/reference/pyright.md).

## Debug with the Python Debugger

With the [Python Debugger extension](https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy) installed, open a Python file and run **Python Debugger: Debug Python File** from the Command Palette (`Cmd+Shift+P` / `Ctrl+Shift+P`). For more control, create a `.vscode/launch.json`:

```json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Module",
            "type": "debugpy",
            "request": "launch",
            "module": "your_package",
            "console": "integratedTerminal"
        }
    ]
}
```

Replace `your_package` with your project's package name. The debugger uses the interpreter from `.venv`, so it has access to all dependencies installed by uv.

> [!TIP]
> To debug a single pytest test, right-click the gutter icon next to the test in the editor and choose **Debug Test**. You can also use the bug icon in the Test Explorer sidebar.

### Pick your next step

- **Adding type checking?** [Configure VS Code for type checking in a uv project](https://pydevtools.com/handbook/how-to/how-to-configure-vs-code-for-type-checking-in-a-uv-project.md) sets up Pylance or ty.
- **Using Cursor instead?** [Configure Cursor for a uv project](https://pydevtools.com/handbook/how-to/how-to-configure-cursor-for-a-uv-project.md) covers the same setup for Cursor.
- **Working in containers?** [Use uv with VS Code devcontainers](https://pydevtools.com/handbook/how-to/how-to-use-uv-with-vs-code-devcontainers.md) configures remote development.
- **Setting up import sorting?** [Sort Python imports with Ruff](https://pydevtools.com/handbook/how-to/how-to-sort-python-imports-with-ruff.md) organizes imports on save.

## Fix common problems

- **"Import could not be resolved" for a third-party dependency.** Run `uv sync` to install all dependencies, then reload the VS Code window. If the missing import is your own package, skip to **Unresolved imports for your own package or a workspace member**.
- **Wrong Python version.** Check that `uv python pin` matches what VS Code shows in the status bar. If they differ, re-select the interpreter from the Command Palette.
- **Ruff settings not applied.** The Ruff extension reads from `pyproject.toml` or `ruff.toml` in your project root. Run `uv run ruff check .` in the terminal to verify the rules match what the extension reports. To confirm the extension loaded the project's Ruff rather than its bundled copy, run **Ruff: Show server logs** from the Command Palette; the startup lines print the path to the executable, which should sit inside `.venv`.
- **Unresolved imports for your own package or a workspace member.** uv installs your project and its workspace members as [editable installs](https://pydevtools.com/handbook/explanation/what-is-an-editable-install.md), which leave a `.pth` file in `.venv\Lib\site-packages` (`.venv/lib/python3.x/site-packages` on macOS and Linux). Open that file and read the first line.

    A filesystem path resolves fine, so the fault is elsewhere: run `uv sync` and reload the window. A line starting with `import` means the {{< term "build-backend" "build backend" >}} installed an import hook, the redirection [PEP 660](https://pydevtools.com/handbook/explanation/what-is-pep-660.md) permits. The pyright engine behind Pylance skips `.pth` lines that begin with `import`, so static analysis stops there.

    uv's own build backend writes a plain path, so most projects never hit this. When yours does, reinstall the package with `--config-settings editable_mode=compat` if it builds with setuptools, which is the only backend that reads that setting; hatchling accepts the flag and ignores it. On any other backend, list the source directory in `python.analysis.extraPaths`. Leave `python.analysis.enableEditableInstalls` alone: Pylance already defaults it to `true`, its `machine` scope means VS Code ignores it in `.vscode/settings.json`, and Pylance describes it as experimental and for internal use.
