Table of Contents
Open Table of Contents
What is uv?
uv is a new, super-fast package manager for Python. It’s designed to be a drop-in replacement for tools like pip and venv, but much, much faster. If you’re tired of waiting for your dependencies to install, uv is for you!
This guide will walk you through the most common commands you’ll use in your day-to-day work.
Installation
First things first, let’s get uv installed on your system.
curl -LsSf https://astral.sh/uv/install.sh | sh
Creating a Virtual Environment
A virtual environment is like a sandbox for your Python project. It keeps all the packages your project needs separate from other projects.
To create a new virtual environment (by default, it will be named .venv):
uv venv
You can also specify a name for your virtual environment:
uv venv my-project-env
Activating the Virtual Environment
Once you’ve created your virtual environment, you need to “activate” it.
source .venv/bin/activate
Installing Packages
Now that you’re inside your virtual environment, you can start adding packages.
To install a single package:
uv pip install requests
To install multiple packages at once:
uv pip install requests flask beautifulsoup4
To install a specific version of a package:
uv pip install requests==2.25.1
Uninstalling Packages
If you no longer need a package, you can easily remove it.
uv pip uninstall requests
Listing Installed Packages
To see all the packages currently installed in your virtual environment:
uv pip list
This is useful for checking what’s installed and their versions. You can also freeze the current state of your environment to a requirements.txt file:
uv pip freeze > requirements.txt
Syncing with requirements.txt
One of the most powerful features of uv is its ability to sync your environment with a requirements.txt file. This ensures that your environment has exactly the packages specified in the file, removing any that aren’t listed.
uv pip sync requirements.txt
This command is great for setting up a project on a new machine or making sure your environment is clean.
Generating a Lock File
uv can generate a uv.lock file, which is a format specific to uv for locking dependencies. This provides a highly optimized and reproducible dependency resolution.
To generate a uv.lock file:
uv lock
You can also generate a requirements.txt style lock file from your project’s dependencies (e.g., pyproject.toml or requirements.in) using the uv pip compile command. This command resolves and pins all dependencies, ensuring reproducible environments.
uv pip compile pyproject.toml -o requirements.txt
If you are using a requirements.in file:
uv pip compile requirements.in -o requirements.txt
Deactivating the Virtual Environment
When you’re done working on your project, you can “deactivate” the virtual environment.
deactivate
Conclusion
And that’s it! You now know the basics of using uv for your Python projects. It’s fast, efficient, and easy to use. Give it a try in your next project and see how much time you save!