Data Science – Setting up a virtual environment for Jupyter Notebook

For doing any project work, we need to install many different packages. We usually see the problem of package conflicts or existing projects stops working because during the new project work setup, mistakenly, we update the existing old packages. Creating the Python Virtual Environment for individual project work is always beneficial to avoid all these kinds of problems. Further, for each project, we can set up all the necessary packages individually.

Before the creation of a python notebook, we need to set up a virtual environment. In this note series, we will work on the Jupyter notebook, and we know that we can select the python environment during the creation of individual notebooks. We will use an anaconda individual edition of python distribution for all the project work. This is one of the most famous python distributions suitable for data science work (non-commercial purpose). Along with we will anaconda-navigator suite, which provides different software IDE for different purposes.

Setting up Virtual Environment in Anaconda Python Distribution

Anaconda provides conda utility software for package management. We will use conda (within anaconda repository) or pip (python public domain) to install all the necessary packages to set up a python virtual environment.

Note: If installation of ipykernel gives error then try to install without using –user option as follows, pip install ipykernel

# Create Virtual Environment
conda create -n my_venv_name

# Activate Virtual Environment
conda activate my_venv_name
# Install ipykernel. It provides the IPython kernel for Jupyter pip install --user ipykernel # Add virtual environment to Jupyter python -m ipykernel install --user --name=my_venv_name # That's all.

Open an anaconda-navigator and click on the Lunch Jupyter notebook to create a new notebook, as shown in the below diagram.

Anaconda Navigator
Anaconda Navigator

Now we can create a new notebook and select the virtual environment as shown in the below diagram.  In the extreme right, we can see three options, Python 3, Python 3.7.0.64 bit, and venv_data8 (it is our virtual environment).

Virtual Environment Selection for New Notebook
Virtual Environment Selection for New Notebook

Reset the environment

Use the below code snippet to deactivate the virtual environment or remove an environment from the Jupyter kernel.

# To deactivate the virtual environment 
conda env remove -n my_venv_name

# To list all the environments
jupyter kernelspec list

# To remove an environment
jupyter kernelspec my_venv_name

Installation of packages using Jyputer notebook

Suppose we want to install the datascience package, then we can use the pip command as conda does not know publically available python packages. Rather it knows only packages available within the anaconda repo.

pip install datascience
Installation of packages
Installation of packages

Summary

This note shows how to set up a virtual environment, create a Jupyter notebook, and install packages using Jupyter notebook. 

 164 total views,  1 views today

Scroll to Top
Scroll to Top