How to Add/Switch Multiple Jupyter Notebook Kernels
Goal
Use Coda or venv to manage packages environment with specific Python versions
What is virtual environment
A cooperatively isolated runtime environment that allows Python users and applications to install and upgrade Python distribution packages without interfering with the behaviour of other Python applications running on the same system.
Reference: Virtual Environments and Packages ⇗
We can choose the Conda way or the standard Python way to achieve it.
Use Conda
Ensure Conda is installed
conda –V
Update Conda
conda update conda
Create a new environment with desired Python version
conda create --name myenv python=3.6.8
Activate the new environment
source activate myenv
In the new environment, install ipykernel
conda install ipykernel
Register a new ipykernel
python -m ipykernel install --user --name myenv
Launch a new Notebook using the myenv
kernel
In the notebook script
!python -V
may return the incorrect version. The system level Python may be returned, and not the version in use in the current kernel environment.
Use virtualenv(venv)
Open Terminal and run, it will create a venv corresponding to the specific_python_version
/path/to/specific_python_version -m venv myenv_py_version
and activate/switch to the venv:
source myenv_py_version/bin/activate
then install iPython kernel package and register a new ipykernel in the current venv:
pip install ipykernel
python -m ipykernel install --user --name=myenv_py_version --display-name "Python <specific_version>"
list available kernel spec for the verification
jupyter kernelspec list
# e.g. output
$ Available kernels:
myenv_py_version /home/jovyan/.local/share/jupyter/kernels/myenv_py_version
python3 /home/jovyan/.local/share/jupyter/kernels/python3
Activate virtualenv
source myenv_py_version/bin/activate
Launch Notebook with specific kernel/venv
Switch Notebook Kernel anytime
Verify Python version in Notebook
# run the code in a cell
import sys
sys.version_info
# e.g. output
# sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)
Install the specific library version in the virtual environment
Activate the specific virtual environment.
The Conda way
source activate myenv
or the standard Python way
source myenv_py_version/bin/activate
Then install the specific library version in this virtual environment. The dependency in this environment is independent from others.
pip install tensorflow==2.1
The library/package version varies with different kernels(virtual environment)