r/learnpython • u/rexdlol • 10h 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?)
15
u/Solonotix 10h 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.
5
u/Ayano-Keiko 9h 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).
4
3
u/mooglinux 8h 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.
3
u/LayotFctor 7h ago edited 7h 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 10h 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 10h 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 9h ago
Try Java ... You will get used to jdk versions...
Or C# , .NET versions
Then uv will makes sense
1
u/Top-Cauliflower-1808 6h 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
1
u/StevenJOwens 5h 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.)
•
u/HommeMusical 46m ago edited 41m 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.
57
u/socal_nerdtastic 10h 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.