🐍

UV: Fast Python Package Manager

Apr 15, 2025

UV: The Game-Changing Python Package and Project Manager

Introduction

  • UV is a Python package and project manager.
  • Claims to be almost 100 times faster than pip in certain situations.
  • Aimed at simplifying management of Python tools like virtual environments and dependencies.

Installation

For Mac/Linux:

  • Use curl or wget, or run:
    pip install uv

For Windows:

  • Use PowerShell or run:
    pip install uv

How to Use UV

  • Open terminal and type uv to see available commands.

Managing Python Versions

  • To list available Python versions:
    uv python list
  • To install a specific version:
    uv python install <version>
    (e.g., uv python install 3.8)
  • To search for a version:
    uv python find <version>
  • To uninstall a version:
    uv python uninstall <version>

Running Python Scripts

  • Simple command to run a script:
    uv run <script_name>
    (e.g., uv run main.py)
  • To specify a Python version:
    uv run --python <version> <script_name>
  • Include dependencies directly in the run:
    uv run --with <dependency_name>
    (e.g., uv run --with rich --with requests)

Managing Dependencies in Scripts

  • Automatically add dependencies by running:
    uv init-script <script_name> --python <version>
  • Add dependencies dynamically using:
    uv add d-script <script_name> <dependency>
  • Dependencies are cached for future runs.

Creating New Python Projects

  • Initialize a new project with:
    uv init
  • This creates files like:
    • .gitignore
    • pyproject.toml
    • main.py
    • README.md
  • Automatically creates a virtual environment.

Managing Project Dependencies

  • To add a dependency:
    uv add <dependency>
  • To remove a dependency:
    uv remove <dependency>
  • The uv.lock file is created to maintain exact package versions.

Syncing Virtual Environments

  • UV automatically syncs the virtual environment with project dependencies when running scripts.
  • Manual sync can be done using:
    uv sync

Final Features

  • Generate a new lock file:
    uv lock
  • Legacy pip usage available with:
    uv pip

Conclusion

  • UV simplifies Python project management and is significantly faster than pip.
  • Encouragement to adopt UV for better dependency and project handling.