In a previous post about Python virtual environment, I introduced to the virtual environment (venv) and how to create it. Today, I will show you how to set up venv inside your Jupyter notebook or Jupyter Lab. Make sure that you have read Python virtual environment before keeping on this post.
The first step, the venv must be installed and activated.
Next, you install jupyter package via pip in order to execute notebook.
Where --name
is an argument for the name of venv in the kernel Jupyter. For the above example, .envNameAny
is the name of venv in kernel Jupyter used for this demo (the name in ipykernel is not necessarily the same name as the venv is generated via python -m venv <envName>
)
To clarify the results, I installed numpy package before running jupyter-notebook. Fig 1 shows UI of the notebook when starting. You choose New > .envNameAny to create your notebook with venv.
Let’s write our first magic code now.