Embracing uv: My Journey with the Superfast Python Package Manager

Embracing uv: My Journey with the Superfast Python Package Manager

Discover how the uv package manager has revolutionized my Python development workflow. From lightning-fast installations to streamlined project management, uv has become an indispensable tool in my toolkit.

Over the past few months, I've had the opportunity to dive deep into the world of Python development, and one tool has consistently stood out: uv. As a developer who often juggles multiple projects, from training language models to fine-tuning existing ones, I needed a package manager that could keep up with my pace. That's where uv comes in.

Discovering uv

Before I discovered uv, I relied heavily on tools like Conda and virtualenv. While these tools got the job done, they often felt clunky and slow. Enter uv, the new package manager written in Rust. I was skeptical at first, but once I started using it, I was blown away by its performance and simplicity.

Why I Love uv

Here are a few reasons why uv has become my go-to package manager:

  1. Incredible Speed: uv is lightning-fast. Whether I'm setting up a new project or installing dependencies, uv handles everything with remarkable speed. This is especially important when I'm working on time-sensitive projects or need to iterate quickly.

  2. Simplified Workflow: uv combines multiple tools into one. Instead of switching between pip, pip-tools, and virtualenv, I can do everything with uv. This has streamlined my workflow and made managing my projects much more efficient.

  3. Seamless Integration: uv works as a drop-in replacement for pip and pip-tools. I didn't need to change any configurations or relearn commands. It just works.

Getting Started with uv

Getting started with uv is straightforward. Here's how I set it up for my projects:

Installing uv

You can install uv using one of the following methods:

  • Using standalone installers:

    • On macOS and Linux:

    • curl -LsSf https://astral.sh/uv/install.sh | sh

    • On Windows:

    • powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

  • Using package managers:

    • With pip:

    • pip install uv

    • With pipx:

    • pipx install uv

    • With Homebrew:

    • brew install uv

Creating a Virtual Environment

Creating a virtual environment with uv is a breeze:

uv venv

By default, this creates a virtual environment in the .venv directory. Activating it is just as simple:

source .venv/bin/activate # On Linux/macOS.venv\Scriptsctivate # On Windows

Setting Up Aliases for Efficiency

To make uv work even more efficiently in the future, I created aliases on my system. These aliases help streamline my workflow by making it easier to activate and deactivate environments quickly. Here’s how you can set up these aliases on your system:

On macOS and Linux

You can add the aliases to your shell profile file (e.g., .bashrc, .zshrc):

alias uvga='uv venv && source .venv/bin/activate' # Generate new environment and activate it

alias uva='source .venv/bin/activate' # Activate an already created environment in the current folder

alias uvd='deactivate' # Deactivate the environment

After adding these lines, run source ~/.bashrc or source ~/.zshrc to reload the profile.

On Windows

You can add the aliases to your PowerShell profile (e.g., Microsoft.PowerShell_profile.ps1):

function uvga { uv venv; . .venv\Scriptsctivate }

function uva { . .venv\Scriptsctivate }

function uvd { deactivate }

After adding these lines, restart PowerShell to apply the changes.

Installing Packages

With uv, installing packages is familiar and fast. For example, to install Flask, I just run:

uv pip install flask

Managing Dependencies

One of the standout features of uv is its ability to lock and sync dependencies effortlessly. To lock my dependencies, I use:

uv pip compile pyproject.toml -o requirements.txt

And to sync my virtual environment with the locked dependencies:

uv pip sync requirements.txt

My Projects with uv

Recently, I've been working on a project to compile a dataset of Yoruba proverbs and related data. I'm using this dataset to fine-tune a large language model (LLM). uv has been invaluable in this process, allowing me to quickly set up my environment and install the necessary packages without any hassle. The speed and efficiency of uv have given me more time to focus on the actual data work and model training.

Looking Ahead

While uv is still relatively new, it's already shown tremendous potential. The performance benefits and streamlined experience make uv a valuable addition to any Python developer's toolkit.

Limitations

While uv supports a large subset of the pip interface, it does not support the entire feature set. In some cases, those differences are intentional; in others, they're a result of uv's early stage of development. For details, see the pip compatibility guide on the project’s GitHub page.

Like pip-compile, uv generates a platform-specific requirements.txt file (unlike, e.g., poetry and pdm, which generate platform-agnostic poetry.lock and pdm.lock files). As such, uv's requirements.txt files may not be portable across platforms and Python versions.

So, if you're looking for a fast, reliable, and easy-to-use package manager, give uv a try. It's transformed the way I manage my Python projects, and I'm confident it can do the same for you. Happy coding!

© 2023 Segun Kolade Cloud Advocate Leader

© 2023 Segun Kolade Cloud Advocate Leader

© 2023 Segun Kolade Cloud Advocate Leader

Get Template