Last Updated on July 14, 2022 by Jay
In this short tutorial, we’ll talk about how to use a Python Virtual Environment (venv) and the Jupyter Notebook. We’ll go through what they are, why, when, and how to use them.
Python Virtual Environment
Most of the time when we use Python, we’ll need to use some third-party libraries, like pandas, plotly, xlwings, etc. These don’t come with the standard Python installation.
Depending on the projects we work on, sometimes our task requires a specific version of a library, but we can keep only 1 version of a library at any given time. For example, if project A requires pandas version 1.0.1, and project B requires pandas version 1.2.0, there’s no way to install both on a computer.
Python Virtual Environment (venv) solves this problem by creating a self-contained copy of Python plus all the libraries. When we create multiple virtual environments, each instance is self-isolated and doesn’t interfere with other environments, so we can have different versions of a library on our computer at the same time.
Create A Python Virtual Environment (venv)
We are going to use a Python module called
venv, which is part of Python standard library and comes with the Python installation. Unlike other libraries, we don’t import venv inside a Python interpreter. Instead, we use it in a Command Prompt / Powershell / Terminal.
Before creating a virtual environment, we need to first decide where to place it, therefore we navigate to the desired folder location first. I’m going to create a new virtual environment in the venv_tut folder:
python -m venv tut_venv
Once it’s created, we should see a folder called “tut_venv” appear in the current directory. Go into the subfolder by using the cd command
cd tut_venv/Scripts #For Windows
cd tut_venv/bin #For Unix or Macos
In the Scripts (or bin) folder, we should see a file named “activate”. Simply in the command prompt type activate to activate the virtual environment. To confirm that the virtual environment is activated, in the command prompt window, we should see (tut_venv) appear in front of the current input line.
Test The Virtual Environment
pandas in this virtual environment and test if it works. Save the following line into a Python file.
import pandas as pd
Note if we attempt to run this code within the IDLE, it might not work, because the current IDLE is not in the virtual environment that we just installed pandas for. Depending on your machine, this current “environment” might not have pandas. To use the correct venv to run our code, we need to execute the code from the console where the venv has is activated. To do that, just type:
This time, the code will run within the correct virtual environment. Now if we need to install another version of pandas, we just need to create a new virtual environment and install it there.
When To Use A Virtual Environment
A virtual environment is particularly useful when you need to constantly switch between different versions of a library. Another instance is when your application requires multiple libraries, it’s known that having too many libraries installed in one environment might cause potential conflicts between libraries.
Although some believe that it’s a good practice to create a new virtual environment for every Python project we work on (because that way the libraries of every project are isolated from the system and each other). I would argue that a separate virtual environment is not necessary unless for larger projects. For example, most of the projects I work on require pandas. Therefore I just have pandas installed system-wide without needing to create a virtual environment every time I start a new project.
Jupyter Notebook is a web-based IDE (interactive development environment) for many programming languages, including Python. In fact, the three core languages that Jupyter supports are Julia, Python, and R. As its name suggests it’s a “notebook”. It means that it can contain both computer code and human-readable content such as text, pictures, etc – just like a physical notebook.
The Jupyter Notebook runs in a web browser, and it’s also interactive. The interactivity is much better than the vanilla Python IDLE in my opinion.
Install Jupyter Notebook
If you already have Python installed on your computer, we can use pip to install Jupyter Notebook.
pip install jupyter
Once the installation finishes, in a console, type
jupyter notebook to open it. You will see that it executes in the console and automatically opens up a browser for the notebook. DO NOT CLOSE THE CONSOLE! The console is the back-end engine, and the browser is merely an interface. If you close the console then Jupyter Notebook will shut down.
Create A Virtual Environment For Jupyter Notebook
Using a virtual environment for Jupyter Notebook is a little different from using it for the vanilla IDLE. In Jupyter Notebook, there’s a thing called IPython Kernel, which is essentially the computational engine that executes the Python code in the back-end. Once we create a virtual environment, we can link it with the kernel so we don’t have to manually activate the venv every time we need it.
To register the
venv with the kernel, we need to pip install another Python module
pip install ipykernel
Once the installation is finished, type the following in the console:
python -m ipykernel install --name=tut-venv
And we’ll see the following message:
Installed kernelspec tut_venv in C:\ProgramData\jupyter\kernels\tut_venv
To test that we registered the venv with ipython kernel successfully. We need to:
- Shutdown Jupyter Notebook
- Deactivate the current venv
- Re-open Jupyter Notebook
- Check in “Open”, we should see the venv name we that we just created ‘tut-venv’. Open a new file with this kernel
- Execute code to check
Remove Virtual Environment From Jupyter Notebook
To remove the venv, type
jupyter kernelspec list in command prompt to confirm the venv name. We’ll see something like the following:
C:\Users\jay\Desktop\PythonInOffice\venv_jupyter_notebook>jupyter kernelspec list
Let’s remove the “tut_venv” that we created for this tutorial. To delete, type
jupyter kernelspec uninstall tut_venv, and we’ll see the following confirmation:
C:\Users\jay\Desktop\PythonInOffice\venv_jupyter_notebook>jupyter kernelspec uninstall tut_venv
Kernel specs to remove:
Remove 1 kernel specs [y/N]: y
[RemoveKernelSpec] Removed C:\ProgramData\jupyter\kernels\tut_venv