# Getting started with uv


[uv](https://pydevtools.com/handbook/reference/uv.md) handles Python installation, project setup, {{< term "dependency-management" >}}, and script execution in a single tool. This tutorial walks through each of those capabilities so you can start using uv for any Python project.

> [!TIP]
> Just want to run a single script, not build a project? Start with [Run your first Python script](https://pydevtools.com/handbook/tutorial/how-to-run-your-first-python-script.md). For the full linting, type-checking, and testing stack, follow [Set up a complete Python project](https://pydevtools.com/handbook/tutorial/set-up-a-complete-python-project.md) after this.

## Prerequisites

You need a terminal (Terminal on macOS, PowerShell on Windows, or any Linux shell). No prior Python installation is required.

[Git](https://git-scm.com/downloads) is optional. If Git is on your {{< term "path" "PATH" >}}, `uv init` initializes a repository in the new project. uv does not install Git itself; install it separately if you want version control. The tutorial steps work either way.

## Installing uv

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

Alternatively, install with Homebrew:

```bash
brew install uv
```

```powershell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
```

Verify the installation:

```console
$ uv --version
uv 0.11.x
```

If you see `command not found: uv`, the installer printed a hint about updating your shell {{< term "path" "PATH" >}}. Open a new terminal window (or restart the current shell) so the updated `PATH` takes effect.

For platform-specific details, see [How to install uv](https://pydevtools.com/handbook/how-to/how-to-install-uv.md).

## Creating a project

### Initialize a new project

```console
$ uv init my-project
Initialized project `my-project` at `/path/to/my-project`
$ cd my-project
```

### Examine the generated files

`uv init` creates a ready-to-use project structure:

{{< /filetree/folder >}}
    {{< /filetree/folder >}}
  {{< /filetree/folder >}}
{{< /filetree/container >}}

Open [pyproject.toml](https://pydevtools.com/handbook/reference/pyproject.toml.md) to see your project metadata:

```toml
[project]
name = "my-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = []

[project.scripts]
my-project = "my_project:main"

[build-system]
requires = ["uv_build>=0.12.0,<0.13.0"]
build-backend = "uv_build"
```

The `[project.scripts]` entry maps the command `my-project` to the `main` function in `src/my_project/__init__.py`. The `.python-version` file records which Python version the project uses. uv reads this file automatically and downloads that interpreter if it isn't already installed. See [What is a Python version file?](https://pydevtools.com/handbook/explanation/what-is-a-python-version-file.md) for more on how this works.

### Run the starter command

```console
$ uv run my-project
Using CPython 3.14.6
Creating virtual environment at: .venv
   Building my-project @ file:///path/to/my-project
      Built my-project @ file:///path/to/my-project
Installed 1 package in 1ms
Hello from my-project!
```

On first invocation, `uv run` creates a {{< term "virtual-environment" "virtual environment" >}} in `.venv/`, installs the correct Python version, builds and installs your project, and then runs the command. Subsequent invocations skip the setup.

Notice the new `.venv/` directory in the project. That is where the project's Python interpreter and installed packages live. uv manages it for you, so there is no need to activate it manually.

> [!TIP]
> `uv init` creates a packaged project with a `src/` layout by default. If you get a flat `main.py` and no `src/`, you are on a uv from before `uv init` became packaged-by-default; run `uv self update`. For a flat `main.py` layout on purpose, libraries, or other structures, see [Understanding uv init project types](https://pydevtools.com/handbook/explanation/understanding-uv-init-project-types.md).

## Adding dependencies

### Add a package

If you see `error: No `pyproject.toml` found in current directory or any parent directory`, you are running `uv add` outside the project. `cd` into the `my-project/` directory you created with `uv init`, then retry.

```console
$ uv add requests
Resolved 6 packages in 143ms
   Building my-project @ file:///path/to/my-project
      Built my-project @ file:///path/to/my-project
Prepared 1 package in 2ms
Uninstalled 1 package in 0.56ms
Installed 6 packages in 3ms
 + certifi==2026.7.22
 + charset-normalizer==3.4.9
 + idna==3.18
 ~ my-project==0.1.0 (from file:///path/to/my-project)
 + requests==2.34.2
 + urllib3==2.7.0
```

This command does three things: updates `dependencies` in `pyproject.toml`, resolves compatible versions and writes a {{< term "lockfile" >}} (`uv.lock`), and installs the package into the project's virtual environment. The `~ my-project` line is your own project being rebuilt and reinstalled, which happens on every `uv add` in a packaged project.

Notice the new `uv.lock` file in the project root. It pins the exact version of every direct and {{< term "transitive-dependency" "transitive dependency" >}}, so anyone running `uv sync` later gets the identical environment. Commit it alongside `pyproject.toml`.

Your `pyproject.toml` now includes:

```toml
dependencies = [
    "requests>=2.34.2",
]
```

### Use the package in your code

Replace the contents of `src/my_project/__init__.py` with:

```python
import requests


def main():
    response = requests.get("https://pypi.org/pypi/requests/json")
    data = response.json()
    print(data["info"]["summary"])  # prints the package summary from PyPI
```

Keep the code inside a `main` function: that is the name `[project.scripts]` points at. Run it:

```console
$ uv run my-project
Python HTTP for Humans.
```

### Add a development dependency

Development tools like linters and test runners are {{< term "dev-dependency" "development dependencies" >}} that don't ship with your project. The `--dev` flag marks a package as development-only:

```console
$ uv add --dev ruff
Resolved 7 packages in 83ms
   Building my-project @ file:///path/to/my-project
      Built my-project @ file:///path/to/my-project
Prepared 1 package in 2ms
Uninstalled 1 package in 0.47ms
Installed 2 packages in 1ms
 ~ my-project==0.1.0 (from file:///path/to/my-project)
 + ruff==0.16.0
```

This adds `ruff` under a development dependency group in `pyproject.toml`. Run it with:

```console
$ uv run ruff check .
All checks passed!
```

### Remove a package

```console
$ uv remove requests
Resolved 2 packages in 3ms
   Building my-project @ file:///path/to/my-project
      Built my-project @ file:///path/to/my-project
Prepared 1 package in 2ms
Uninstalled 6 packages in 10ms
Installed 1 package in 1ms
 - certifi==2026.7.22
 - charset-normalizer==3.4.9
 - idna==3.18
 ~ my-project==0.1.0 (from file:///path/to/my-project)
 - requests==2.34.2
 - urllib3==2.7.0
```

This updates `pyproject.toml`, regenerates `uv.lock`, and uninstalls the package from the virtual environment.

## Running code and tools

`uv run` is the primary way to execute anything inside your project's {{< term "virtual-environment" >}}. It checks that all required packages are installed and up to date before running your code, so you never hit a missing-package error.

Run your project's command:

```console
$ uv run my-project
Hello from my-project!
```

Run a module. `uv run -m http.server 8000` starts a local web server and blocks the terminal. Press `Ctrl+C` to stop it:

```console
$ uv run -m http.server 8000
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
```

Run a tool installed as a dependency:

```console
$ uv run ruff format .
2 files left unchanged
```

Notice that `uv run ruff format` reports every file it touches, even when nothing changes. The two files are your module and `pyproject.toml`, and the `left unchanged` count confirms ruff ran and found nothing to reformat.

To run a CLI tool that isn't part of your project (a one-off invocation), use [uvx](https://pydevtools.com/handbook/reference/uvx.md):

```console
$ uvx cowsay -t "hello uv"
  ________
| hello uv |
  ========
        \
         \
           ^__^
           (oo)\_______
           (__)\       )\/\
               ||----w |
               ||     ||
```

If you see `usage: Cowsay [-h] ... error: the following arguments are required: -t/--text`, you omitted the `-t` flag. `uvx cowsay` requires `-t` before the message text.

For a deeper comparison, see [When to use uv run vs uvx](https://pydevtools.com/handbook/explanation/when-to-use-uv-run-vs-uvx.md).

## Managing Python versions

uv downloads and manages Python interpreters directly. No separate tool like [pyenv](https://pydevtools.com/handbook/reference/pyenv.md) is needed.

### List available versions

```console
$ uv python list
cpython-3.14.0-macos-aarch64-none                 <download available>
cpython-3.13.5-macos-aarch64-none                 /Users/you/.local/share/uv/python/cpython-3.13.5-macos-aarch64-none/bin/python3.13
cpython-3.13.5+freethreaded-macos-aarch64-none    <download available>
cpython-3.12.7-macos-aarch64-none                 <download available>
...
```

The output lists every Python build uv can manage. Versions already on disk show a path; versions marked `<download available>` will be fetched on first use. Your output will differ based on platform, architecture, and which versions you have already installed.

### Install a specific version

```console
$ uv python install 3.12
Installed Python 3.12.7 in 80ms
 + cpython-3.12.7-macos-aarch64-none (python3.12)
```

If the version is already installed, uv prints `Python 3.12 is already installed` instead and exits without changes.

### Pin a version to your project

The pinned version must satisfy the `requires-python` constraint in `pyproject.toml`. If `requires-python = ">=3.13"`, pinning to `3.12` fails with `error: The requested Python version `3.12` is incompatible with the project `requires-python` value of `>=3.13`.` Loosen `requires-python` first, or pin a compatible version.

```console
$ uv python pin 3.12
Pinned `.python-version` to `3.12`
```

This writes `3.12` to the `.python-version` file. The next time you run `uv run`, uv uses that version (installing it first if necessary). Open `.python-version` to confirm the change; it contains a single line with the pinned version.

### Test against a different version

You can run a single command with a different Python version without changing the project pin. If uv does not have that version on disk, it downloads it first:

```console
$ uv run --python 3.11 my-project
Installed Python 3.11.13 in 3.21s
 + cpython-3.11.13-macos-aarch64-none (python3.11)
Hello from my-project!
```

Subsequent invocations with `--python 3.11` skip the download and run immediately. If you see `error: The requested Python version 3.11 is incompatible with the project's Python requirement`, the `requires-python` field in `pyproject.toml` excludes that version. Either loosen `requires-python` or pick a version within the allowed range.

## Choose your next step

Continue with a project or explore a specific workflow:

- [Complete guide to uv](https://pydevtools.com/handbook/explanation/uv-complete-guide.md) covers workspaces, lockfile strategies, and advanced configuration
- [Create your first Python project](https://pydevtools.com/handbook/tutorial/create-your-first-python-project.md) builds a text analysis tool from scratch using uv
- [Set up Ruff for formatting and checking your code](https://pydevtools.com/handbook/tutorial/set-up-ruff-for-formatting-and-checking-your-code.md) adds linting and formatting to a uv project; see the [Ruff reference](https://pydevtools.com/handbook/reference/ruff.md) for its features and configuration
- [Setting up testing with pytest and uv](https://pydevtools.com/handbook/tutorial/setting-up-testing-with-pytest-and-uv.md) sets up a test suite with parametrized tests and coverage
- [How to translate pip commands to uv](https://pydevtools.com/handbook/how-to/how-to-translate-pip-commands-to-uv.md) maps familiar pip workflows to uv equivalents
- [uv reference page](https://pydevtools.com/handbook/reference/uv.md) for a concise overview of features, pros, and cons
- [Modern Python Tooling Checklist](https://pydevtools.com/tooling-checklist.md) collects these defaults on one page for when you standardize a project or team template
