When we are working in a python project we work with a number of dependencies and libraries written by other people. So, that we don't have to re-invent the wheel each time so to speak. This can help with faster code output and increase in efficiency all around.
For example; we may use Flask library for web development or requests library for making HTTP requests. And these library change over time as the folks that maintain these third-party libraries add new features and amend the old ones as new ways of doing things are discovered.
So, when we install them for one of our projects, we download certain version of that specific library. And certain months or years down the road, when we again try to install the same library we may as well download another version of the same library with entirely different functionality.
Therefore, when we update our code with the new version, the code may all breakdown or not run as we had expected because the way the code should be written in the new version has completely changed or the functionality that we had implemented in our code through that library has been removed in the new version. As you may expect this can create a major issue for production grade software.
What Are Python Virtual Environments?
To resolve this issue, virtual environment exist. They essentially help us to separate the dependencies of one project from another. In this way, two project that use same library can have completely different version as requirement for it to run smoothly. This is often the case when we start our project in different time.
In simple terms, virtual environment is a self-containing folder structure that is separate from your system installed versions of dependencies and libraries. When you create a virtual environment, it will have its own pip to install libraries and other dependencies as well as your own version of Python interpreter.
Why do we need virtual environment?
Using a virtual environment is essential specially for large scale project. It will make your application become self-contained and also provide you with a number of benefits. Such as:
1. You can work with projects that use different version of Python interpreter in the same machine as each new environment is isolated.
2. As your application is self contained, it won't interfere with system installed Python interpreter and its various libraries.
3. It is lot easier to work with other developers as you can quickly package your application share it with others.
4. You can provide anyone with the list of dependencies for your project. This makes it extremely easy for any other developers to replicate and install all dependencies required to run your project within a virtual environment.
Conclusion
Virtual environment gives you the ability to essentially isolate your Python projects from the system installed Python interpreter and other various libraries and package within the system. This ability of virtual environment is essentially what makes it so easy for us to make open source project on platform like GitHub so easily replicable.
While creating virtual environment may not matter much when you are developing a small personal project. It is an absolute requirement when you are working on scalable and large scale Python projects. Hence, it is necessary for any good Python developer to know how once can create and work with virtual environments and understand its need in a production grade application.