Python's virtual environments how to create and manage separate virtual environments for your Python projects and why is important in utilizing them.
My first "mystery" (it was wiked at that time) when I started to learn Python was virtual environments.
I understood the concepts, but struggled first to understand clearly their role and then to configure them on my PyCharm but once I got it, I loved it.
The goal of this writing would be to share my view with you on below aspects:
Python virtual environments are just that, pockets(folders) in your computer which segregate different version of software that you can use to develop and test at ease; it creates isolated contexts to keep dependencies required by different projects separate so they don’t mix with other projects or system wide packages. Simple put it, virtual environments is the best way to have different Python projects separated virtually.
When you are a beginner, you tend to have a single system python interpreter (usually in base environment) were you will dump all your packages and it is ok-ish until you learn the hard way, that it's not.
Having only one source (providers in term of packages) place for all your work is fine until one point when you want to start building greater things and you'll be working with many programs having different complex dependencies, and yes in that moment of time learning to jingle with virtual environments will be a tremendous step in the right direction; thus is better to start from beginning to know about virtual environments.
Quoting from Python documentation 28.3.1 bottom Note: "A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.A virtual environment is a directory tree which contains Python executable files and other files which indicate that it is a virtual environment."
This means that when you activate your project virtual environment, your project virtually becomes independent of the system Python and its modules/packages etc (your project becomes self-contained); your virtual environment has its own pip to handle libraries, its own folder structure (different tools to handle virtual environments), its own Python interpreter with the required version which will be used to interpret your current or future program.
In some cases virtual environments are a make, brake or take deal. Having this in place it's just a simpler way to write code or automated tests agaist different versions of Python, packages or a combination of both.
The importance of virtual environments becomes much apparent when you have various Python projects on the same machine that has dependencies on different versions of same package.
In my case I've discovered (of course after reading blogs and articles and having to play with them) that the best practice, for each project I build is to generate a dedicated environment and never install packages globally ( it’s a common and effective technique to be used in Python development and not only).
Let me give you an example.
Let's say you are working on 2 different projects which requires 2 different Pandas packages 1.5 and 2.0. This would lead to compatibility issues because Python cannot simultaneously use multiple version of the same package in the same virtual environment.
Another example, would be, you are pushing code to a place where are used certain versions of packages and you have to mirror your development environment to match that certain requirements.
Python virtual environments has 2 main components:
To provide a visual demonstration of the above alambicated phrase, please see below:
The above picture its a good representation of what you can have on you system, when you create a Python virtual environment which is a tree folder containing a specific python version together with the 3rd parties libraries. As far as I know there is no limitation on the number of virtual environment which you can make on your machine (in the end are folders with files or other folders).
Enough theory on this subject, lets deep dive into possible tools to aid/use virtual environments.
There are a couple of options which can be used to create this so called virtual environment and we will take a look at the most important one:
Each of these above tools offers distinct advantages and potential challenges faces when used.
The best choice often depends on the specific requirements of your project (and you will enjoy this statement in all the blogs or documentations you'll be reading), the complexity of the dependencies and the preferences of the development team always dictates which of the above can or should be used.
Happy PiPing!