DEV Community

Cover image for Getting Started with uv: A Fast Python Package Manager for Web Development
Priyanshu Jha
Priyanshu Jha

Posted on

1

Getting Started with uv: A Fast Python Package Manager for Web Development

⚡ Getting Started with uv: A Fast Python Package Manager for Web Development

Managing Python packages efficiently is crucial for web development projects. Enter uv, a blazing-fast Python package and project manager written in Rust by the creators of Ruff.

In this post, we'll explore how to install uv, use it for package management, and integrate it into a web development project.


🚀 What is uv?

uv is a modern Python package manager designed to be a drop-in replacement for pip and pip-tools. It offers:

  • Speed: Significantly faster installations and dependency resolution.
  • Unified Interface: Manage packages, virtual environments, and Python versions seamlessly.
  • Compatibility: Works with existing requirements.txt and pyproject.toml files.

🛠️ Installing uv

You can install uv using various methods. Here's how to do it using the standalone installer:

For macOS and Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

For Windows (PowerShell):

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Enter fullscreen mode Exit fullscreen mode

Alternatively, install via pip:

pip install uv
Enter fullscreen mode Exit fullscreen mode

Note: Installing via pip may require a Rust toolchain if prebuilt binaries are unavailable for your platform. citeturn0search1


📦 Basic Usage

Creating a Virtual Environment:

uv venv
Enter fullscreen mode Exit fullscreen mode

This command creates a virtual environment in the current directory.

Installing Packages:

uv pip install flask
Enter fullscreen mode Exit fullscreen mode

Installs the Flask package into your virtual environment.

Installing from requirements.txt:

uv pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Installs all packages listed in your requirements.txt file.

Compiling Requirements:

uv pip compile
Enter fullscreen mode Exit fullscreen mode

Generates a requirements.txt file with pinned versions from your pyproject.toml.

Syncing Environment:

uv pip sync
Enter fullscreen mode Exit fullscreen mode

Synchronizes your virtual environment with the requirements.txt file.


🌐 Integrating uv into a Web Development Project

Let's say you're working on a Flask-based web application. Here's how you can set up your project using uv:

  1. Initialize a New Project Directory:
   mkdir my-flask-app
   cd my-flask-app
Enter fullscreen mode Exit fullscreen mode
  1. Create a Virtual Environment:
   uv venv
Enter fullscreen mode Exit fullscreen mode
  1. Activate the Virtual Environment:
  • On macOS/Linux:

     source .venv/bin/activate
    
  • On Windows:

     .venv\Scripts\activate
    
  1. Install Flask:
   uv pip install flask
Enter fullscreen mode Exit fullscreen mode
  1. Create a pyproject.toml File:
   [project]
   name = "my-flask-app"
   version = "0.1.0"
   dependencies = [
       "flask"
   ]
Enter fullscreen mode Exit fullscreen mode
  1. Compile Requirements:
   uv pip compile
Enter fullscreen mode Exit fullscreen mode

This generates a requirements.txt file with pinned versions.

  1. Run Your Flask App:

Create an app.py file:

   from flask import Flask

   app = Flask(__name__)

   @app.route('/')
   def hello():
       return "Hello, World!"

   if __name__ == '__main__':
       app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

Run the application:

   python app.py
Enter fullscreen mode Exit fullscreen mode

🧪 Additional Features

  • Installing Specific Python Versions:
  uv python install 3.12
Enter fullscreen mode Exit fullscreen mode

Installs Python 3.12 in an isolated environment. citeturn0search5

  • Running Tools Without Installation:
  uvx ruff
Enter fullscreen mode Exit fullscreen mode

Runs the ruff tool without installing it globally.

  • Publishing Packages:
  uv build
  uv publish
Enter fullscreen mode Exit fullscreen mode

Builds and publishes your package to a registry like PyPI.


📚 Resources


💬 Conclusion

uv streamlines Python package management, making it faster and more efficient—especially beneficial for web development projects. Its compatibility with existing tools and additional features like Python version management and tool execution without installation make it a valuable addition to your development workflow.

Have you tried uv in your projects? Share your experiences and tips in the comments below!


Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (0)

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

👋 Kindness is contagious

If you found this article helpful, a little ❤️ or a friendly comment would be much appreciated!

Got it