Use UV to run Python packages and programs without installing

Overview

Astral's uv utility simplifies and speeds up working with Python virtual environments. But it has some other superpowers, too: it lets you run Python packages and programs without having to formally install them, all by way of one simple command. This video shows off how this can work for anything from a simple one-file program to a full project with a pyproject.toml file.
Featuring Serdar Yegulalp:
https://www.linkedin.com/in/serdar-yegulalp-136a483/

Register Now

Transcript

Astral's UV tool makes it fast and easy to set up Python environments and projects. It also gives you another superpower. You can run Python programs without needing to manually install any of their requirements. Let's take a look at how this works. Consider this program.

It uses an external library, Markdown, to convert Markdown format text into HTML. I don't have markdown installed on the system and I have no virtual environment configured for this project. So markdown doesn't get recognized by the llinter and trying to run it with Python doesn't work.

Now I can try to run with UV as well but by default that still won't do. What I need to do is pass a flag to UV the width flag that provides the names of libraries I want to use from Pippi with this program.

So if I run uv run with markdown and then the name of our program, you will see very quickly that it fetches markdown from pipi, installs it into a self-contained environment and runs the program from that environment. All of UV's downloads are cached.

So if I run the program again, it just uses the local cache and saves us the step of downloading anything. This behavior is really useful if we need large complex packages for our program. Here's another example where we're using numpy.

When we run this program with UV, uv run with numpy, there's a fairly long fetch and setup cycle for the library because numpy is a few dozen megabytes. It's not a trivial inclusion.

But subsequent runs will set up and launch faster because we already have numpy cached in our dependency cache. Now here in another directory, I have a full project with a pirotol file and an entry point command. This is my Conway's game of life simulation.

Longtime viewers of this channel ought to remember this one. Now, if I want to run this with UV, here's what I do. I type UV run Conway life. That last one being the name of the entry point command for this particular project.

And if I do this, UV downloads all the needed external dependencies, creates a virtual environment in my project directory to place them in, and even builds the Syon extension module that I have for this project.

I should note that this last step, the compiling of the extension modules, that's only going to work if you already have a C compiler set up on your system. I have MSVC already installed. So, UV wouldn't obtain compiler infrastructure for you for a project like this.

That would honestly be a really complicated job with a lot of potential pitfalls. It's better to do it yourself. And I've talked before about there are many easy ways to do that, especially if you're on Microsoft Windows, as I am here.

Now, this is only the first and the most obvious things that you can do with UV run and external libraries. In a future video, I'm going to show some even more advanced tricks that you can pull thanks to some new features that were recently added to Python itself.

And that's it for this episode. If you liked it, leave a comment below. And don't forget to follow Dev with Zertar and Infoworld on Facebook, YouTube, and infoworld.com. [Music]