> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/prog/python/virtual-environments-uv.md).

# Virtual environments - uv

An extremely fast Python package and project manager, written in Rust.

* A single tool to replace `pip`, `pip-tools`, `pipx`, `poetry`, `pyenv`, `twine`, `virtualenv`, and more.
* [10-100x faster](https://github.com/astral-sh/uv/blob/main/BENCHMARKS.md) than `pip`.
* Provides [comprehensive project management](https://github.com/astral-sh/uv#projects), with a [universal lockfile](https://docs.astral.sh/uv/concepts/projects/layout#the-lockfile).
* [Runs scripts](https://github.com/astral-sh/uv#scripts), with support for [inline dependency metadata](https://docs.astral.sh/uv/guides/scripts#declaring-script-dependencies).
* [Installs and manages](https://github.com/astral-sh/uv#python-versions) Python versions.
* [Runs and installs](https://github.com/astral-sh/uv#tools) tools published as Python packages.
* Includes a [pip-compatible interface](https://github.com/astral-sh/uv#the-pip-interface) for a performance boost with a familiar CLI.
* Supports Cargo-style [workspaces](https://docs.astral.sh/uv/concepts/projects/workspaces) for scalable projects.
* Disk-space efficient, with a [global cache](https://docs.astral.sh/uv/concepts/cache) for dependency deduplication.
* Installable without Rust or Python via `curl` or `pip`.
* Supports macOS, Linux, and Windows.

## Handle packages

### Install packages

To install a Python package with `uv`

```bash
uv tool install <pkg_name>
```

To install a Python package with a specific Python version

```bash
uv tool install --python 3.x.y <pkg_name>
```

### Uninstall packages

To uninstall a package previously installed with `uv`

```bash
uv tool uninstall <pkg_name>
```

### List supported Python versions

To list the Python versions supported by `uv`

```bash
uv python list
```

## Handle scripts

To "handle" standalone scripts with `uv`

```bash
uv add --script <script_name>.py -r requirements.txt
```

This will add [Inline script metadata](https://peps.python.org/pep-0723/) to the script.

Then run the script with

```bash
uv run <script_name>.py
```

Or set the shebang to

```bash
#!/usr/bin/env -S uv run --script
```

and run with `./<script_name>.py` as normal.

The virtual environments will be installed under `~/.cache/uv`.

## Other features

### Updating

To update `uv`

```bash
uv self update
```

## Resources

Python UV for Hackers - 0xdf: <https://www.youtube.com/watch?v=G36QXtBXKBQ>

uv - Docs: <https://docs.astral.sh/uv/>

uv - GitHub: <https://github.com/astral-sh/uv>
