r/learnprogramming • u/dcfan105 • Jan 31 '23
Python Why is it important to use virtual environments for Python projects but not for other languages, such as C++, R, etc.?
If I understand correctly, the reason for using separate environments is so that different versions of the same library don't interfere with each other, as some projects may require particular versions of specific libraries. Or you might even have some libraries that only work with earlier versions of Python, etc. That makes sense. My question is, why is something that's apparently only relevant to Python? I never heard about using virtual environments with R, C, C++, or any other programming language. Why is there not an issue with different versions of libraries potentially interfering with each other in R, for example, but there is with Python?
84
u/[deleted] Jan 31 '23
Dont know about C C++ but i work in rust and all crates(libraries, modules) are installed locally in the project folder, exactly like node modules, now the problem with python is, whenever you do a pip install the packages install to a centralised folder where all python projects can use them, this creates inconsistencies beacuse if Project A requires Module ver1.0 but Project requires Module ver5.0. The solution is do a pip install of ver5.0 but then you risk breaking Project A if the module has had major work done (which in most cases is the case because of depracation and whatnot) So yeah get into the habit of using venv whenever you have a project that requires specific versions of packages
38
u/POGtastic Jan 31 '23
List of other languages that I've used where dependencies are collected per-project instead of globally:
cargo)cabal,stack)sbt)rebar3)mix)cypm)quicklisp)npm,yarn)spago)Languages that I've used where dependencies are collected at the system level:
That... That's about it.
11
u/_uwu-uwu_ Jan 31 '23
In R, you usually mimic this behavior by using separate R projects with their own library directory to install packages. Otherwise, the default directory where packages are installed gets bloated or you run into version issues. Yet there isn’t anything for R, to my knowledge, that works as well as virtual environments.
9
u/Western-Relative Jan 31 '23
As others noted it’s not just Python that’s peculiar in this way. Other programming languages work the same way or have similar peculiarities.
The real reason has to do with symbol resolution. Each language needs a way to associate
foo()in its source code with a piece of executable code, variable, constant, whatever…. Python uses virtual environments to scope symbols appropriately, JS uses a modules folder, languages like go install them locally and put everything in the main executable unless told otherwise, etc. Other languages use the system dynamic linker and share libraries with C/C++, and some are hybrids.The reason why C/C++ and other languages that rely on the system loader are shared is because (a) it predates the concept of a virtual env and (b) the system only loads that library once and shares it across all processes on the system (unless you’re doing something special and take appropriate actions). Languages like NodeJS load it for each process. While it may not seem wasteful now since memory is plentiful (mostly) a while ago that was not possible and sharing code meant you could do lots more.
Take a look at
ld, the program that does this on GNU systems, and maybe some of its command line options (-land-L). Also it might be interesting to read https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html (from a Linux perspective — I’m sure Windows has something similar).5
u/pipocaQuemada Jan 31 '23
The terminology for the build systems differs across languages.
In C and C++, people talk about statically linked executables vs dynamically linked ones. And the problem that venv solved was called "dll hell" on windows for C, or more generically "dependency hell".
The basic issue is that if you have a global store of all of the dependencies in your projects with a single global version of each dependency, different projects can rely on different versions, and installing the dependencies of one project can break another.
The basic solution to this is to have per-project dependencies. Possibly using a global cache of versioned dependencies.
In python, the tool to achieve that is called virtual environments. In Haskell's cabal build tool, the initial approach was sandboxes (storing the cache of dependencies in each project), and was replaced with "nix-style local builds" (using a global versioned cache). Npm distinguishes between local and global package installs. Some build systems don't have a particular name for local builds because that's the only kind of build they support.
So the difference is really only terminology and python's tooling for local builds being unusually clunky for historical reasons. In "normal" languages you can just run either a single command like
cargo buildwhich will parse your dependencies, fetch any you don't have and compile them and your project, or two commands likenpm installto install dependencies locally thennpm runto run your project with them.1
u/NappyTime5 Feb 01 '23
Pythons are invasive in a lot of places, like the Florida Everglades, so you have to be careful about accidentally releasing one into the wild. Always use a virtual environment
3
u/nutrecht Jan 31 '23
All languages have issues with libraries, versions and how these can conflict. All of them have systems that deal with this so that different projects don't clash with each other. They all deal with it in different ways. Python has it's own 'way', but it's really not different from other languages. It just solves the same problem in a slightly different way.
1
u/mfro001 Feb 01 '23
other languages have very similar problems called dependency hell to deal with
0
u/CodeTinkerer Jan 31 '23
C and C++ are pretty old languages, and people didn't think about creating individual environments (maybe due to lack of memory?). These languages also had fairly crude build systems (makefiles).
But you're right in that Python emphasizes it more. You can write Python code without environments, though. It just means that you need to share Python things with all your programs.
0
u/slashdave Jan 31 '23
You can and often do use virtual environments for C++ and R.
0
u/Xaxxus Feb 01 '23
the same reason tons of companies still use java 8.
Someone wrote some mission critical application in python 2 and never updated it. And as time went on it got more and more bloated, and more and more complex. And now nobody can update it without taking down the company's systems.
So it just sits there forever on python 2.
The virtual environments let people who have to work on that monstrosity also work on newer stuff as well.
-4
u/Zealousideal-Mail276 Jan 31 '23
Python venv are used to install modules in a local directory and to prevent pollution on your whole system.
C++ has no packages, no modules, no anything, it's a language. You can compare Python with Conan though but it's a passive system (i.e. no pollution of the whole OS), and you build in a specific directory (same as a venv).
-1
u/Lazy-Evaluation Feb 01 '23
So say I want to obliterate a Linux kernel with my idea of a scheduler or some nonsense they ask of you in college. Ha, well, jokes on them, I'm capable apparently. My partner even more so.
How do I not crash every time? I'm insane. I plan it out. And...fail.
Funny story, it was all planned out in my mind, and my stupid task scheduler did the exact same thing the existing scheduler already did. To the best of our ability to load the thing with jobs. I'd imagine given a real world heavy load the weaknesses would shine through.
I'm getting way off track. How or why would I virtualize such things? The why, well, seems self evident. Screw around with the kernel and bad things happen. The how? I've been a big fan of virtualbox. Not a fan of Oracle, but that's before my time.
Something more mundane? Get yourself employable. Test driven development, containerized applications, automatic deployment, automatic testing, automatic building, etc.
-5
u/XFajk_ Jan 31 '23
Because C and C++ is compilled I think thats the main reason why
-2
u/JayTheLegends Jan 31 '23
Isn’t this because there’s two major releases of python. 2 being there still because old systems and 3 the latest update..