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
andpyproject.toml
. - Built-in virtual environment management.
- Cross-platform support (Windows, Linux, macOS).
Step 2: Installing UV
- For macOS Users:
- If you have Homebrew installed, run the following command:
brew install uv
- If you have Homebrew installed, run the following command:
- 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
- Create a new project directory:
mkdir my_project cd my_project
- Initialize the project with UV:
uv init
- This command generates essential files:
.gitignore
pyproject.toml
(replacessetup.py
,setup.cfg
, andrequirements.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
- To add a package:
uv add package_name
- To install a specific package version:
uv add package_name==version
- To view the dependency tree:
uv tree
- 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
anduv.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
andpip 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.