Motivation
Creating a virtual environment is a good practice. It isolates the dependencies of a specific project from the rest of the packages globally installed on your operating system. As a Data Scientist, this can be beneficial when working on different projects which generally require different dependencies.
At the end of this conceptual blog, you will be able to:
(1) Create virtual environments using virtualenv
, and conda
(2) Connect your virtual environments to your Jupyter notebook.
Create Your Virtual Environment
Each one of the tools is used to manage python packages and the environment. The main difference between the two is that virtualenv
is used to manage Python packages for a single project, while conda
is a more general-purpose package management tool that can be used for multiple projects and languages such as Java, R, Python, etc.
To begin, you will need to have Python installed on your computer. Once this is done, we can proceed with the creation of the environments using pip
, the Python package manager.
Create an environment using virtualenv
virtualenv
can be installed using as follows from your CLI or prompt command:
pip3 install virtualenv
After a successful installation, you can proceed with the creation of the virtual environment using the following syntax:
virtualenv virtual_env_1
From the previous syntax, virtual_env_1
corresponds to the name of your virtual environment.
The next step is to activate it to be able to use it. The activation depends on your operating system:
# On Mac
source virtual_env_1/bin/activate
# On Windows
virtual_env_1Scriptsactivate
Running one of the previous commands should show the name of the virtual environment parenthesis like this:

(virtual_env_1)
means that virtual_env_1
is considered to be the current environment. You can now proceed with the installation of your packages and manage them within the environment. Here is an example:
pip install pandas # to install pandas library
Using conda
The first step to creating the virtual environment is to install conda
on your system. If not done you can download and install Anaconda or Miniconda which already includes conda
.
Before creating the environment, let’s deactivate the previously created environment with the deactivate
command:
deactivate
Now, you can create your new environment virtual_env_2
with conda
and Python 3.8
by running the following command:
conda create --name virtual_env_2 python=3.8
Finally, you can activate the environment as shown below. The activation process is the same no matter your operating system:
conda activate virtual_env_2
This command activates virtual_env_2
and makes it the current environment. Below is the expected output.

We can deactivate the environment using this syntax:
conda deactivate
Connect Your Virtual Environment to Jupyter Notebook.
As Data Scientists, most of our analysis and model training are primarily done within Jupyter notebooks.
Wouldn’t be nice to be able our virtual environments right from the notebooks?
This is the main goal of this section. It explains how to connect the previous environments to jupyter notebook.
The first step is to install ipykernel
. This package allows us to manage kernels from different environments. The installation is done using pip
:
pip3 install --user ipykernel
Using ipykernel
we can add any virtual environment using this general syntax:
python -m ipykernel install --user --name=[my_virtual_env]
[my_virtual_env]
is the name of an existing virtual environment.
So, let’s add both of our virtual environment virtual_env_1
and virtual_env_2
:
# Add the first virtual environment
python -m ipykernel install --user --name=virtual_env_1
# Add the second virtual environment
python -m ipykernel install --user --name=virtual_env_2
The previous commands add your environments! Now, if you run Jupyter notebook, you should be able to see both environments when creating a new notebook.
# Run your notebook
jupyter notebook
From the notebook, we should see our kernels as shown below and we can decide which one to use:

Conclusion
Congratulations! 🎉 You have learned how to create virtual environments using conda
and virtualenv
. Also, you’ve discovered how to connect your environments to Jupyter notebook.
If you like reading my stories and wish to support my writing, consider becoming a Medium member. With a $ 5-a-month commitment, you unlock unlimited access to stories on Medium.
Feel free to follow me on Medium, Twitter, and YouTube, or say Hi on LinkedIn. It is always a pleasure to discuss AI, ML, Data Science, NLP, and MLOps stuff!