Inside a Jupyter Notebook I have managed to install a Python kernel with
!python -m ipykernel install --user --name other-env --display-name "Python (other-env)"
as informed here and it is available with other kernels on the menu Kernel → Change kernel and
!jupyter kernelspec list
will also show them
Available kernels:
avenv C:\Users\Full Name\AppData\Roaming\jupyter\kernels\avenv
chatterbot C:\Users\Full Name\AppData\Roaming\jupyter\kernels\chatterbot
othervenv C:\Users\Full Name\AppData\Roaming\jupyter\kernels\othervenv
python3 C:\Users\Full Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\share\jupyter\kernels\python3
Then I try to install Python a package using
%pip install a_package
as given here that said
with % (instead of !) it will install
a_packageinto the current kernel (rather than into the instance of Python that launched the notebook).
But what I have got that it installs a_package to all kernels, or %pip list will list the same installed packages in all kernels.
Is there a way to install Python package only to an active Jupyter Notebook kernel?
Short answer
Path of python executable in JSON file of Jupyter kernel should point to python executable in related virtual environment.
Long answer
After some tries the steps can be summarized as follow.
Python virtual environment
c:\venvs\butiran)butiraninvenvs(folder for all virtual environments)pipas in hereipykernelas in hereJupyter kernel
ipykernelfor Jupyter Notebook as in herepython.exeC:\\Users\\Full Name\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exeC:\\venvs\\butiran\\Scripts\\python.exeJupyter Notebook
Open existing notebook or create a new one.
Change kernel from menu Kernel → Change kernel → Python (butiran).
Install
a_packageusing only in active kernelor with
will give
while show the package in active kernel
will give