r/learnpython • • 4d ago

What exactly is uv? Why other programming languages like C++ doesn't needs it

Hello, im a c++ programmer and after installing some tools made in python with pipx. For context, pipx uses uv, i had this question. I'm not the smartest progammer on earth and i couldn't understand why Python needs a separate environment for dependencies.

I never such tool like that as a c++ programmer (i don't know if it's what im thinking, but in C++ some devs install the dependencies inside de project dir so u just "cmake" it. uv is like this?). I'm kinda new to programming, so if someone can explain it to me, i'd be very grateful!

Sorry if this question sounds too beginner. I really couldn't understand this tool and curiosity is stronger than me...

Thanks! (and also, uv does more than separating environments, right?)

43 Upvotes

33 comments sorted by

84

u/socal_nerdtastic 4d ago

C++ is generally shipped as a compiled executable. But Python is generally shipped as source code. So to run someone else's python code you need to remake the correct environment with the required dependencies. This in itself does not require uv or virtual environments. However there is a chance that the environments clash for different projects, that is that the dependencies and versions for one project won't work for another project. venvs keep projects separate, each in it's own environment, and uv is a tool to manage environments quickly and easily.

3

u/rexdlol 2d ago

Thanks for this explanation and for the care!

In C++, when the code is compiled (with static libs), the libs get compiled alongside the source code, so the dependencies are already in the binary, right? So this problem extends to C++ when using shared librarys? Thats interesting, i didn't know this (beginner in c++ too lol).

Well, anyway, thanks again

3

u/socal_nerdtastic 2d ago

So this problem extends to C++ when using shared librarys?

Well kinda. It extends if you are sharing your C++ code with other developers. But not if you are distributing your code to users. The key difference is that python code is shipped to the user as source code.

3

u/jaimemiguel 3d ago

Like a container

1

u/BigGuyWhoKills 11h ago

I have a project that needs charinfo below a certain version, and another that needs it above that version.

22

u/Solonotix 4d ago

UV is a tool chain for Python development. The first thing it does is provide an interface for managing Python versions, as well as tracking dependencies for a given project. It can then install those dependencies in a central cache, properly versioned, and locked via a uv.lock file. It also provides some scripting capabilities for actions directly linked to the project.

So, in a C++ context, it handles your compiler version, downloading and verifying your vendored packages as well as their pre-requisites, and it takes the place of a Makefile.

2

u/rexdlol 2d ago

Python is pretty convenient. Maybe ill learn python; it is on my list of things to learn (alongside with rust, i find it really cool too)

edit: Thanks!

10

u/Ayano-Keiko 4d ago

C++ makefile / cmake(cmake exactly generate makefile) is similiar tool as uv as a package manage tool in python. But it more like rust cargo syntax (Exactly uv is written in rust).

1

u/rexdlol 2d ago

Understood!
The comments here really helped me out

7

u/mooglinux 4d ago

uv combines functionality of a lot of different tools that exist in nearly every language ecosystem. The most important is dependency resolution: identifying the correct versions of each dependency library and gathering up their dependencies and checking for conflicts. uv conveniently handles creating the environment and installing all required packages. Critically it ensures the environment has the correct version of each package.

Environments are a Python-specific thing: it’s the collection of all of the source modules that the interpreter is given access to at runtime. Different programs have different dependencies, so you can’t just dump everything into a single global installation without inevitably breaking something. It’s the sort of problem you wouldn’t run into with C/C++ unless you were dynamically linking to shared libraries; uv is the Python equivalent of ensuring all the DLL files are exactly the same as what a program was compiled against.

1

u/rexdlol 2d ago

Thanks for this explanation!

People here helped me out with this. Because python is shipped with source code (generally unlike c++, which the librarys get compiled in the binary), the environment that it was made needs to be the same for the person thats trying to run it (or smth like that.)

4

u/HommeMusical 4d ago edited 4d ago

I never such tool like that as a c++ programmer

I've been programming C++ for decades.

There are plenty of such tools in C++, Conan being the most prominent example.

Dependency management is hard. C++ never really solved that issue, and thus adding C++ dependencies is still a pain point.

C++ modules attack a similar issue in another way, but they're also not ready for primetime.

I'm not the smartest progammer on earth and i couldn't understand why Python needs a separate environment for dependencies.

Don't run yourself down - asking "dumb" questions is a great way to learn. Doubting is always a good way to learn, it shows you're actively confronting the material. Keep asking such questions!

The build in C++ is intimately hooked up with your specific OS, whereas Python has virtual environments, venvs, which are really light and compartmentalize all the dependencies for this one project.

That's really great, but it means that you can't use these tools if you are out of a specific environment. And you don't want to install tools into your system Python, because you want to keep that completely unchanged - often there are safeguards preventing or discouraging this.

So pipx creates an additional environment for tools that aren't tied to a venv, and makes sure you can see that environment everywhere.

uv does more than separating environments

Yes, a large number of things, particularly to do with building, publishing and running your Python project in a controlled and low-typing way.

1

u/rexdlol 2d ago

Thanks for this message and this explanation!

Now i can understand all of this. People in this subreddit are really cool.

Again, thanks!

6

u/future_lard 4d ago

Everyone explains what uv does but not why uv instead of venv or conda

4

u/Dobsus 3d ago

This one is best understood by trying to use venv or conda

1

u/eW4GJMqscYtbBkw9 2d ago

I use venv every day. Now what?

1

u/Dobsus 1d ago

Try uv

Of the two conda is the worst offender though...

2

u/scan-horizon 3d ago

Uv is faster

1

u/proverbialbunny 3d ago

It's better that's why. Use venv or conda if you want, but uv is newer and as best I can tell it is better in every way. uv does have a learning curve like venv has. That's it's only downside.

1

u/rexdlol 2d ago

Guys, thanks for all the responses and messages. Now i can understand this concept. I really appreciate!

3

u/LayotFctor 4d ago edited 4d ago

Because C and C++ are old. Modern languages include the build manager, test runner, package manager etc, e.g. Rust's cargo. Python also lacks such "all included" tools, which is why someone needed to create uv to the modern standard.

1

u/Bach4Ants 4d ago

I think of it as a Python project manager. It will handle your dependencies and create a dev environment for you inside the project repo, including installing Python itself. It will also help you get your package ready to distribute.

Here's a good explainer, straight from the horse's mouth: https://astral.sh/blog/uv-unified-python-packaging

1

u/FatDog69 4d ago

There are dozens or hundreds of libraries you can pull in to help you with your python project. Each of these has a different version. You version of python also has a version.

So for many projects you want to install all the things that might change into a project folder. This way later updates don't break your project.

It also creates a .txt file that documents all these version that create a working version of your project. Someone else can then create the identical environment if you give them the file.

Since all these versions are copied to your project folder - you can update Python on your system, and over the years use newer versions of many packages. But it wont break your older projects.

1

u/ninhaomah 4d ago

Try Java ... You will get used to jdk versions...

Or C# , .NET versions 

Then uv will makes sense

1

u/Top-Cauliflower-1808 4d ago

The C plus plus comparison is fair but not exact. Python defaults to one shared global install so environments stop one tool from breaking another. uv bundles environment creation dependency resolution and tool installs while pipx uses it to keep command line tools isolated.

1

u/StevenJOwens 4d ago

As u/socal_nerdtastic points out, when you ship somebody a program you developed in C++, you're shipping them the entire program as a (mostly) single file. When you compile your program, code from whatever libraries you need is combined into that single file. There are usually little to no external dependencies, particularly for libraries. So there's little to no need for you to manage those dependencies.

Python is an interpreted language*. When you run a python program and the source code uses a library, the python interpreter then looks for the file that contains that library, reads in the file, and interprets/uses that source code. And the library source code may in turn reference other libraries, etc.

So you have to manage installing and configuring those libraries, and you have to manage including them with the program you're distributing, or at least including a formalized definition of the dependencies, etc.

Various tools have been developed to simplify various aspects of this whole set of problems. Virtual environments are one, package installers and package dependency management tools are another.

uv is a from-the-ground-up rewrite of several of these tools, with a focus on performance. Some of that is that uv was written in Rust. Most of it (according to the uv creators) is simply better design, i.e. paying more attention to the dependencies in the process, managing those better, etc. Another aspect of uv is that it was written to mimic the command line interface of (some of) those tools, because lots of people already know how to use those tools.

(* This opens the door to a bunch of caveats and ambiguity about compiled versus interpreted. TL;DR, it's more complicated than that. Python is not "truly" interpreted, almost no languages today are truly interpreted, not in the sense that the strings in the source code are parsed and executed one at a time. They almost always do a just-in-time compilation-like step when first loading the code, often to some intermediate format, some flavor of bytecode, etc.)

1

u/ohnoitssobig 3d ago

> i couldn't understand why Python needs a separate environment for dependencies.

It does not need it but it is a very nice thing to have. I have one env for vim code server, another env for some tools, third env for the project and they are all independent. Each of them may (or may not) use the same python executable. If you try to do it in c world you end up with containers but python venvs are much simpler and require very little maintenance. Funny thing is, you may have and use .so/.dll inside python venv.

> some devs install the dependencies inside de project dir so u just "cmake" it. uv is like this?

Dependency lists are a part of individual packages, not venv itself. But you may just list some packages in requirements.txt and ask to create a venv with all these. It will try to resolve specific versions in a package manager style and install all of them.

In C++ it is much more complex than just "install dependencies in the project". You may compile dynamically, statically, against system libs, against pre-existing object files, against other compilation targets/sub-projects.

> uv does more than separating environments, right?)

Yes: there is a python -m venv which is a "reference" venv implementation. uv builds on top of it. Honestly, not much difference in practice except that uv can download python binaries such that python itself becomes a dependency which you can specify (e.g. specific python version). Has some fine controls, faster, etc

1

u/Sufficient-Tale6131 3d ago

pipx and `uv tool install` are basically the same idea btw: every Python CLI app gets its own hidden environment so their dependencies can't clash.

The reason Python needs that at all is that imports are resolved at runtime from whatever site-packages the interpreter points at. Two apps wanting different versions of the same library would break each other in one shared install. C++ mostly dodges this because libraries get linked in at build time. If you want a C++ comparison for a uv project, vcpkg in manifest mode is closer than plain cmake: per-project dependency list plus pinned versions.

1

u/Chippors 2d ago

Compiled languages resolve dependencies except for a small number of shared libraries at build time. Code that has installed and run for months isn't suddenly going to refuse to run with import errors because some environment variable changed, or a different tool installed a different version of the dependency elsewhere and it's now getting picked up.

1

u/Confident_Hyena2506 4d ago

Conda is what you want for c++, not uv.