This is an introduction of how to handle multiple virtual environments when using Python with Jupyter Notebook.
In the following, we will add a Python 3.8 virtual environment as a sample.
Let's say Python3.8 can be run with the python3.8
command. If not installed,
Download and install it from https://www.python.org/downloads/ etc.
First, create a virtual environment called venv38 and activate it. If you want to use a different virtual environment name, read venv38 below as appropriate.
python3.8 -m venv venv38
source venv38/bin/activate
Install the required libraries appropriately with pip install ...
.
Add a virtual environment to the kernel.
pip install ipykernel
python -m ipykernel install --name venv38
Let's use jupyter in a non-virtual environment. Once, exit the virtual environment with deactivate
.
Start Jupyter Notebook with jupyter-notebook
.
If you select "venv38" from "New" on the upper right, you can start anew in the virtual environment of venv38.
First, open your existing notebook. You can change the kernel with Change kernel in the Kernel menu of Jupyter Notebook. If you can change it, the kernel display on the upper right will change.
In the following, it will be executed on the added virtual environment.
You can see a list of kernels and their paths with jupyter kernelspec list
.
You can remove the added virtual environment with jupyter kernelspec uninstall venv38
.
In the following, it will be executed on the Jupyter Notebook of the added virtual environment.
For example, let's say you want to add a NumPy installation to venv38.
Normally, you can do it with ! Pip install numpy
, but this will install it on your console instead of venv38.
If you want to install from Jupyter Notebook to virtual environment, do as follows. The path is not the path confirmed above, but the path of the virtual environment created first.
!source /path/to/venv38/bin/activate; pip install numpy
that's all
Recommended Posts