What is a virtual environment
In Python a virtual environment is an isolated environment where you can install specific packages you will need for a specific project you are working with.
So for example you might have a project where you are using OpenCV 3.4.2 and a different one where you are using OpenCV 4.5.2 and all you will have to do is to activate the correct environment every time you need to work with each of these.
Since the environments are completely isolated from each other you can have different versions of the requirements on each virtual environment.
It is a good idea to always create a clean environment when starting a new project, do not work on the system environment nor use the same environment for different projects, it will save you a lot of trouble.
There are many ways to work with virtual environments in python, here we will learn how to do it using a special python module called “virtualenv”
Installing the module “venv”
If you do not have pip installed you can do it with this command in the terminal:
sudo apt-get install python3-pip
Assuming you already have pip installed you can install virtualenv with this line:
sudo pip3 install virtualenv
Creating an empty environment with venv
Make sure you are in the folder where you want your virtual environment to be created.
You can create your first virtual environment like this:
python3 -m venv myenv
Where “-m” specifies we want to run a specific module of python which in this case is “venv” and “myenv” is the name of the environment we want to create.
2. How AI Will Power the Next Wave of Healthcare Innovation?
3. Machine Learning by Using Regression Model
4. Top Data Science Platforms in 2021 Other than Kaggle
This will take just a couple of seconds, and you should be able to see a newly created folder:
This will create a new folder called myenv which will contain the files of the newly created environment.
Activating the python virtual environment
So now the new virtual environment has been created but we still need to activate it, to do so just type:
This will activate the environment and show as part of the path in the terminal like this:
Now you can proceed to install new packages inside the virtual environment.
Installing packages to the virtual environment
As you can see, if you check the packages installed with pip, the environment will be empty:
So it is time to start installing your requirements.
But before we do it, as you can see pip is out of date (for some reason it is always out of date!)
So lets first update it with the following command:
python3 -m pip install — upgrade pip
This will update pip and get rid of the warning.
So now just proceed to install your packages with pip install, for example lets install numpy and then list the installed packages with pip list
pip install numpypip list
Here we can see how now we have an clean environment that only contains numpy installed.
Deactivating the environment
To deactivate the environment all we have to do is type in the word deactivate like this:
This will make the (myenv) from the path disappear, which mean the environment has been deactivated.
Attaching a virtual environment to Jupyter Notebook
Great work so far!, now you know now to create, activate, install packages and deactivate a python virtual environment.
Personally I work a lot of times with Jupyter notebooks and I want to have my environments attached to my Jupyter notebook kernel and a lot of times this does not work out of the box.
The good news is that I will tell you here exactly how you can do it and it only takes one command to do it!
First make sure you got ipykernel installed:
pip install ipykernel# Replace VIRTUAL_ENV_NAME and “Name you want to show for the Jupyter kernel”python -m ipykernel install — user — name VIRTUAL_ENV_NAME — display-name “Name you want to show for the Jupyter kernel”
Did you find this article useful?
Share your comments and experience!, let us know what works and what did not work for you.
Make sure to give this post some claps (30 to 50 or so is a good number!) if you enjoyed this post and want to see more.
And to keep up to date make sure to follow, until next time!