Create a custom jupyter kernel for your venv.
This package creates jupyter kernels for the
venv it is installed
You maintain multiple virtual environments via python's
venv or the
virtualenv package. You want
to be able to switch between them from within a single jupyter
installation. How do you do this?
You need a (user-) global jupyter installation. The recommended
approach for that is to use
pipx to install jupyter as a standalone
tool. Jupyter can handle multiple different kernels, even for the same
python version, and they are easily maintained with the
jupyter kernelspec command. The only nuissance is to create and install the
kernel.json files manually for each venv. That's where
It is recommended to maintain your python versions with
pipx. Both of these packages can be installed with the
usual package managers such as
Specifically, install and temporarily activate a recent python version with pyenv, e.g., via
pyenv install 3.9.10 pyenv shell 3.9.10
Then install jupyter using pipx as per
pipx install --install-deps notebook jupyter jupyter_contrib_nbextensions
which places it in its own virtual environment, all managed by pipx. You can call jupyter from the command line now.
Install a custom kernel for a VENV
Every time you want to add a custom virtual environment as a kernel option to your jupyter notebook server, follow these steps:
- If you haven't done so yet, create and activate the venv as per usual, e.g., via
pyenv shell 3.10 # we want to use this particular python version pip -m venv .venv . .venv/bin/activate pip install --upgrade pip pip install <list of packages here> or pip install -r requirements.txt
- Install venv-kernel as per
pip install venv-kernel
- Create and install the custom jupyter kernel
venv-kernel install --name "MyProject" --description "Virtual Environment for MyProject using Python 3.10"Here the
--descriptionare optional and default to the direcory name of the virtual environment.
- Start/restart your jupyter notebook server. You should now see the kernel "MyProject", which uses the Python version of your virtual environment and has access to all the packages installed in it.
If for any reason you want to uninstall a kernel created by this package, you can simply do so using the commands
jupyter kernelspec list
to identify the kernel in question and then delete it via
jupyter kernelspec remove
If you are within a virtualenv that has
venv-kernel installed, you
can also use
to see if there's currently a kernel installed that corresponds to the current venv, and
to remove it.
There are other packages that provide similar or related functionality, and these may or may not serve your purposes better than this package, which is designed solely to meet the author's needs. These packages include:
- callisto: Create jupyter kernels from virtual environments
- envkernel: Jupyter kernels manipulation and in other environments (docker, Lmod, etc.)
- ssh-ipykernel: A remote jupyter ipykernel via ssh
Copyright 2021 Björn Rüffer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size venv_kernel-1.0.6-py3-none-any.whl (6.4 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size venv-kernel-1.0.6.tar.gz (6.0 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for venv_kernel-1.0.6-py3-none-any.whl