Get Started with UV: The Modern Python Package Manager (Full Tutorial) #aitutorialforbeginners

3 min read 10 hours ago
Published on Sep 05, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial guides you through using UV, a modern Python package manager designed for speed and simplicity. By following these steps, you'll learn to install UV, manage Python projects, handle dependencies, and understand how to utilize its features to enhance your development workflow.

Step 1: Understanding UV

  • UV is a high-performance package manager written in Rust, aimed at improving upon traditional tools like pip, virtualenv, and venv.
  • Key advantages include:
    • Ultra-fast installation and dependency resolution.
    • A single tool for installing, locking, and syncing dependencies.
    • Full compatibility with requirements.txt and pyproject.toml.
    • Built-in virtual environment management.
    • Cross-platform support (Windows, Linux, macOS).

Step 2: Installing UV

  1. For macOS Users:
    • If you have Homebrew installed, run the following command:
      brew install uv
      
  2. For Windows and Linux Users:
    • Visit the official UV GitHub repository or documentation for installation instructions tailored to your operating system.

Step 3: Initializing a UV Project

  1. Create a new project directory:
    mkdir my_project
    cd my_project
    
  2. Initialize the project with UV:
    uv init
    
  3. This command generates essential files:
    • .gitignore
    • pyproject.toml (replaces setup.py, setup.cfg, and requirements.txt)

Step 4: Understanding pyproject.toml

  • This file is crucial for defining your project’s metadata and dependencies.
  • Learn the TOML syntax, which is simple and human-readable.

Step 5: Managing Python Versions with UV

  • To list available Python versions:
    uv python list
    
  • To install a specific version:
    uv python install <version>
    

Step 6: Installing and Managing Dependencies with UV

  1. To add a package:
    uv add package_name
    
  2. To install a specific package version:
    uv add package_name==version
    
  3. To view the dependency tree:
    uv tree
    
  4. To remove a package:
    uv remove package_name
    

Step 7: Understanding the uv.lock File

  • The uv.lock file captures exact resolved package versions.
  • Ensure this file is checked into version control for consistency across environments.

Step 8: Running Python Scripts with UV

  • Execute your Python scripts directly using:
    uv run script_name.py
    

Step 9: Migrating Projects to UV

  • Use pyproject.toml and uv.lock files to transfer projects to new environments.
  • UV will automatically set up virtual environments and install dependencies.

Step 10: Comparing UV with Traditional Methods

  • UV simplifies virtual environment creation and dependency management compared to traditional methods like using venv and pip freeze.

Conclusion

By mastering UV, you can significantly optimize your Python development process, streamline package management, and eliminate common pitfalls like ModuleNotFoundError. Start using UV today to leverage its powerful features and enhance your coding efficiency. Consider exploring more advanced features and keep your projects up to date with the latest dependency management practices.