I’m diving headfirst into using uv
in my daily grind to be more efficient in
my pile of work. So, I’m throwing myself into all sorts of random scenarios.
On this journey, I’ve stumbled upon some [bugs][uv-bug] and nifty features like a super-powered cache. But, hold up! The real point of this post is how an innovation affects to the production process.
Intro
Surprise, surprise! I’m an economist. In econ 101, we talk about the law of diminishing returns, which, in plain-speak, means you can’t boost productivity just by tossing more resources at something. Like, hiring a bunch more people won’t help if the room’s already crowded and everyone’s squished together like sardines.
But hey, remote work’s a game-changer! It lets us crank up productivity without being limited by physical space. Yet, there are other constraints like our code repositories (our virtual goldmines).
Many cool innovations only happened because some earlier innovation paved the way. And without the former, thinking about the possibilities is like trying to catch smoke with a butterfly net (not always, but very often).
That’s how I see uv
. It’s totally the future of Python package managing.
So far…
Until now, starting with Python meant jumping through the same old hoops: setting up a virtual environment (venv), activating it, installing all your dependencies, and then cranking out code. We just accepted this as normal, like a hidden tax.
Some of you might have a generic venv with your fave libs all set up, ready to roll. But that’s just putting off the inevitable task of defining a proper venv
- requirements when you share the code.
You do this because tinkering with environments is such a time-suck!
Too lazy for this…
Let me share a fun example of this:
Recently, I wanted to whip up a newsletter for this blog. But none of the existing solutions tickled my fancy, so I thought, “Hey, why not make a monthly summary post with links?” It was a grand idea but not quite a newsletter (no auto emails or subscriptions). Just when I was ready to scrap the idea, I stumbled on Thorsten Ball’s blog. His clever fix was using Substack. Brilliant!
So, I only needed to grab links from my blog programmatically ‘cause spending hours on this isn’t my jam.
I wrote something like this:
|
|
That code pulls the RSS feed from my blog, filters posts for a specific month, and builds a simple HTML file that I can copy-paste into my Substack post.
I had all the bits but didn’t want to piece them together because setting up a
venv for such a simple task sounds like work (I only needed one external
dependency, jinja2
).
Let’s try uv
for this…
But with uv
, all the puzzle pieces fall into place:
|
|
Abracadabra! Done!
|
|
the first line adds script dependencies to my Python script, in something called PEP-723, like so at the top of the file:
|
|
And, with the second command, it creates a venv using the right Python version, installs the dependencies, and runs the script, all in a snap!
|
|
I foresee a time where updating script dependencies will be as easy as one command, but for now, this is pure magic!
Wrap Up
With these examples, I’m just trying to show you that you don’t have to stick
to the same old ways unless there’s a darn good reason. uv
opens up this
groovy new way of running Python scripts and supercharges all you Python peeps!
I used to write loads of bash and Go scripts due to dependency chaos (thanks a
lot, macOS—I never had these issues on Linux). Also, pipx
wasn’t my thing
since it plays badly on macOS.
uv
is mind-blowing; it works beautifully on Mac and Linux.
Share Like a Boss
It gives you an awesome way to share scripts and (venvs) all packed in one neat file. Picture this: you whip up a little code, slap on the dependencies, toss it into a GitHub gist, share it, and then run:
|
|
Boom! Magic!
Extra
And let’s wrap it up with one last cheeky note. It also lets you play around with testing algorithm performance on different setups (Python 3.8, 3.9, 3.10…, with all sorts of numpy, pandas versions…) so you can flex your benchmarking muscles and pick the top-performing champ.
Picture a script like this where we can mix and match the Python and numpy versions used:
|
|
Let’s roll with this:
- Whip up a template
- Cook up multiple versions of that template
- Race against the clock
|
|
The beauty of this? You don’t even need the tool pre-installed anymore. We can
just whip up an abstract alias and let uv
do all the heavy lifting! 🎉
|
|