Virtual environment: Solving real life need of using multiple python versions concurrently
Deepak Kumar
Propelling AI To Reinvent The Future || Mentor|| Leader || Innovator || Machine learning Specialist || Distributed architecture | IoT | Cloud Computing
Introduction
Laymen explanation
If you are web programmer and you use Django as your framework, then you might have Imagined scenario where you are working on two web based python projects and one of them uses a Django 1.9 and the other uses Django 1.10 and so on. Both versions will have different dependencies of python libraries. However, since both versions are visible to both project, you may have noticed conflict.
In such situations virtual environment can be really useful to maintain dependencies of both the projects.
Technical explanation
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.
Why we need this?
Python, like most other modern programming languages, has its own unique way of downloading, storing, and resolving packages (or modules). While this has its advantages, there were some interesting decisions made about package storage and resolution, which has lead to some problems—particularly with how and where packages are stored.
A third party packages installed using easy_install or pip are typically placed in one of the directories pointed to by site.getsitepackages:
Now consider the following scenario where you have two projects:
ProjectA and ProjectB,
Both of which have a dependency on the same library, ProjectC. The problem becomes apparent when we start requiring different versions of ProjectC. Maybe ProjectA needs v1.0.0, while ProjectB requires the newer v2.0.0, for example.
This is a real problem for Python since it can’t differentiate between versions in the site-packages directory. So both v1.0.0 and v2.0.0 would reside in the same directory with the same name.
This is where virtual environments and the virtualenv/venv tools come into play.
When it should be used?
Virtual Environment should be used whenever you work on any Python based project. It is generally good to have one new virtual environment for every Python based project you work on. So the dependencies of every project are isolated from the system and each other.
Alternative approach via container technology
Running project using container orchestration will solve this problem. Note that container infra ensures that each container has its own unique dependency.