How to use uv
1 What is uv?
uv is “An extremely fast Python package and project manager, written in Rust.”1
I’ve been using uv for some projects and I really notice an improvement in the velocity when I’m installing Python packages in comparison with another tools like pip. For example in the Figure 1 we can see the different in time installing the same package with different packages manager where uv is the fastest.
2 Installation and configuration
We have different options to install uv go ahead to the documentation Installing uv to find the best option for you project.
3 Usage
3.1 Basic commands
Install specific Python version
uv python install 3.12 3.11List Python versions installed
uv python listRun a Python script
uv run script.py3.2 Start a project
Starting a project will create the following files:
- main.py
- pyproject.toml
- uv.lock
- .python-version
Commands:
# Start the project hello-world
uv init hello-world
# Start the project hello-world with Python 3.12
uv init hello-world --python 3.12
# Usage the current folder as a project
uv init
# Execute the project
uv run main.py 3.3 Working with virtual environments
Create a virtual environment (this command will create .venv folder)
uv venvInstalling packages on the .venv
# A package
uv pip install PACKAGE_NAME
# A requirements.txt
uv pip install -r requirements.txtActivate the virtual environment for Bash and ZSH shell
source .venv/bin/activate3.4 Dependencies management
Add a public library with specific version
uv add 'request==2.31.0'Add a library using git repository (could be useful for private repositories)
uv add git.https://github.com/...Install with requirements.txt and constraints.txt files
uv add -r requirements.txt -c constraints.txtRemove packages
uv remove requests3.5 Running servers
Run a flask server
uv run -- flask run -p 3000Run a quarto server in localhost
uv run -- quarto preview mysite --port PORT --host 0.0.0.0Render quarto document
uv run -- quarto render mysite3.6 Using integrations
Using Jupyter Lab2
uv run --with jupyter jupyter labUsing Docker Images with uv installed 3
docker run --rm -it ghcr.io/astral-sh/uv:debian uv --help