This article will guide you through setting up UV, an extremely fast Python package manager written in Rust. It serves as a single, comprehensive tool for managing Python versions, pip dependencies, virtual environments, and much more, all with a focus on speed and ease of use.
Why Choose UV?
UV is a powerful, all-in-one solution that simplifies your Python workflow. It combines the functionality of multiple tools into a single, cohesive interface. Here are some of its standout features:
- Unified Tooling: Manage Python versions, virtual environments, and package installations without juggling different utilities.
- Incredible Speed: Built in Rust, it offers a significant performance boost over traditional package managers.
- Familiar Commands: It provides a
pipinterface, allowing you to use familiar commands likepip install. - Project Initialization: Quickly scaffold a new Python project with a standard structure.
- Script-Specific Dependencies: Package dependencies for single Python scripts, making them portable and easy to run in isolated environments.
Installation
To install UV, simply run the following command in your terminal. It will automatically detect your operating system and install the appropriate version.
curl -LsSf https://astral.sh/uv/install.sh | sh
After the installation, you may need to restart your shell for the uv command to become available. Once you do, you can verify the installation by typing:
uv --version
Core Features Explained
UV offers a range of commands to streamline your development process:
uv init: Initializes a new Python project, creating apyproject.toml, amain.py, and aREADME.mdfile.uv venv: Creates a virtual environment, optionally with a specific Python version.uv pip install: Installs packages into your environment using a familiar pip-like syntax.uv run: Executes a Python script within an isolated environment, automatically managing its dependencies.uv add: Adds dependencies to yourpyproject.tomlfile.uv python install: Installs different versions of Python, similar topyenv.
Running Single-File Scripts
One of UV's most convenient features is its ability to run single Python scripts with their dependencies defined directly in the file. For example, consider a script that uses the requests library.
You can add a special comment to specify the dependency:
example.py
```python
/// script
requires-python = ">=3.8"
dependencies = [
"requests<3",
]
///
import requests
response = requests.get("https://astral.sh") print(response.status_code) ```
Next time you want to execute it, you can use uv to run the script. It will automatically create an isolated virtual environment, install the required packages, and then run the code. This makes sharing and running single-use Python scripts with a few dependencies incredibly simple.
Getting Started: A Practical Example
Let's walk through creating a new project and managing it with UV.
1. Create a Project Directory
First, create an empty directory for your project.
mkdir test-python-project
cd test-python-project
2. Initialize the Project
Run uv init to set up the project structure.
uv init
This command creates a main.py file, a pyproject.toml file, and a README.md. The pyproject.toml will contain basic project metadata and a section for dependencies.
pyproject.toml
toml
[project]
name = "test-python-project"
version = "0.1.0"
description = "Add your description here"
dependencies = []
requires-python = ">=3.8"
3. Create a Virtual Environment
Next, create a virtual environment for your project.
uv venv
This command creates a .venv directory in your project folder using your globally available Python version.
4. Activate the Environment
To activate the new environment, use the source command provided in the output.
source .venv/bin/activate
Your shell prompt should now indicate that the virtual environment is active.
5. Install Packages
To add packages, use the uv pip install command. Let's install numpy.
uv pip install numpy
UV will quickly resolve and install numpy and its dependencies. You can verify the installation by checking the installed packages.
uv pip list
This will show that numpy is now available in your environment. You can continue to install more packages as needed and run your Python files directly with uv run.