Let’s talk about a very important topic for improving code compatibility – Virtual Environments in Python. You might have heard of developers talking about how some code seems to work on their system, but, doesn’t seem to work on a different one?
The well known “it works on my system”.
Why do we face code compatibility issues?
Why does a piece of code work on the developer’s system? The answer is pretty simple because the modules that they have are compatible with the code.
Now, the question is why doesn’t it work on your system then? That too, has a simple answer, because some of the modules aren’t compatible with the code.
What do I mean by that? You see, a lot of the times, once a module is updated, some of the existing features that were applicable before, do not work.
In these cases, when the module is installed in another system, the updated version is unable to process the code given even though it is the same module.
A good example of this, is one where a patent for some of the existing features in Python OpenCV was discovered and the support for those features was disabled in the next version of the module.
The patent has expired now, and was worked on during GSoC.
So, to resolve this issue, we use virtual environments in Python.
What are Virtual Environments in Python?
Virtual environments are the environments that we build in order to create an environment which is optimized for our specific purpose. We install, and utilize modules that are necessary for our project, and solely for our project.
This allows our environment to be a lot less heavy, and light weight.
Setting up a Virtual Environment
Let’s make our own virtual environment in Python, designed for data science in this article,
1. Installing the Virtual Environment Module
With the release of Python 3.3, the virtual environment module
venv, has been added by default into the Python standard library. As such, we do not have to install any module to work with virtual environments.
In case you are using a version of Python that is before 3.3, you should look into installing the virtualenv module.
While Anaconda, by default provides us with such an environment, along with the package to create virtual environments, we’ll be working with the default virtual environment module provided by Python for this article.
2. Creating Virtual Environments
We use a simple command to create virtual environments in Python.
You can either specify the directory as to where you wish to install the environment, or just provide it’s name to install in the current directory.
## python -m venv path\to\nameofenvironment
python -m venv D:\Pyth-Grip\testenv
# macOS and Linux
python3 -m venv testenv
After installation, we can now proceed to activating the virtual environment.
3. Activating the Virtual Environment
These simple commands above, allowed us to create a virtual environment. Upon accessing this environment, the file structure you will find will be similar to this,
In this directory, the
pyvenv.cfg is the configuration for the particular environment based on the existing version of Python in your system.
Scripts directory is where we should proceed into next, as it contains the
activate command which allows us to activate the virtual environment.
From outside this directory, these are the instructions we will need to follow to activate the virtual environment,
cd testenv # the name of your environment
After this sequence of commands, you will notice that the environment is active when the name of the environment is in rounded brackets,
In my case, the command prompt displays the activated environment in such a manner,
4. Setting up the Virtual Environment
Now, we have our very own environment which we can use solely for data science.
But, wait! The modules are yet to be installed as this is only a fresh environment without any of our required modules.
Let us install some Python modules to work with,
# Installing the required version of pandas
pip install pandas==1.1.3
# Installing the latest version of scikit-learn
pip install scikit-learn
As you can see, we can install exactly which version of the module we want in this virtual environment, keeping our base environment clean and lightweight.
5. Deactivating the environment
Once we’re done with working on a particular environment, and wish to use another or use the base environment, it’s important to know how to exit the current virtual environment.
This can be done through one simple command.
And, that’s it!
You’re now good to go, and work on specific environments, which won’t conflict with your project!