Post

uv Python package and project manager

uv Python package and project manager

uv is a Python package and project manager that’s written in Rust and is popular for its speed and efficiency. Its features include: dependency installation and management, creation and reproduction of virtual environments, and it also helps with building and publishing Python packages.

Here are some commands in order to use uv.

Reference post.

Installation

For Windows

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

For Linux + macOS

1
wget -qO- https://astral.sh/uv/install.sh | sh

or Using PyPI

1
pipx install uv

Check installation

1
uv --version

Restart or refresh the terminal if the binary doesn’t register immediately.

Create a Python project

uv can create a project directory using the following command:

1
uv init <project_name>

It’s also possible to create the directory structure in an existing project. Go to the current project directory and initialize.

1
uv init

This doesn’t work if there is an existing pyproject.toml file. It doesn’t overwrite existing main.py, Git repository, or README.md file.

Running the entry-point script creates a virtual environment .venv for the project.

1
uv run main.py

The script name does not have to be main.py for uv run to work.

Manage dependencies

Install a dependency, e.g. requests Python library.

1
uv add requests

uv will automatically update the list of dependencies in the pyproject.toml file.

To add an exiting requirements.txt file to uv:

1
uv add -r requirements.txt

List all installed dependencies:

1
uv pip list

Update dependency

To update an existing dependency:

1
uv add --upgrade requests

To remove an existing dependency:

1
uv remove requests

Manage development dependencies

Use the --dev flag to denote a dependency as part of development, e.g. the pytest library

1
uv add --dev pytest

Locking and syncing dependencies

lock means capturing a project’s dependencies so that the project’s working environment becomes reproducible. These are all written into the project’s lockfile.

sync is the process of installing the required packages from the lockfile into the project’s environment.

Both locking and syncing are automatically handled by uv. When uv run is invoked, the project is first locked and synced before the command is run.

To manually lock and/or sync the project, run the following, respectively:

1
2
uv lock
uv sync

More

For steps on how to build and publish Python packages using uv check the Real Python post: Managing Python Projects With uv: An All-in-One Solution

This post is licensed under CC BY 4.0 by the author.