Skip to main content

Create a custom jupyter kernel for your venv.

Project description

venv-kernel

PyPI - latest version PyPI - License PyPI - supported Python versions PyPI - Downloads

Summary

This package creates jupyter kernels for the venv it is installed in.

Use-case scenario

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 venv-kernel comes in.

Suggested workflow

One-time setup

It is recommended to maintain your python versions with pyenv and jupyter with pipx. Both of these packages can be installed with the usual package managers such as apt-get or brew.

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:

  1. 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
    
  2. Install venv-kernel as per
    pip install venv-kernel
    
  3. Create and install the custom jupyter kernel
    venv-kernel install --name "MyProject" --description "Virtual Environment for MyProject using Python 3.10"
    
    Here the --name and --description are optional and default to the direcory name of the virtual environment.
  4. 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.

Removal

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

venv-kernel list

to see if there's currently a kernel installed that corresponds to the current venv, and

venv-kernel clean

to remove it.

Similar packages

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

MIT License

Copyright 2021-2024 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

venv_kernel-1.0.11.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

venv_kernel-1.0.11-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file venv_kernel-1.0.11.tar.gz.

File metadata

  • Download URL: venv_kernel-1.0.11.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/22.5.0

File hashes

Hashes for venv_kernel-1.0.11.tar.gz
Algorithm Hash digest
SHA256 8aa0b63ac4dab2a967ae7c00ff6f91b9cfb67550c76408714aec52dcf81b5f11
MD5 642032c60cbf7e64ad882c0ad6531e53
BLAKE2b-256 9c7868ce6b222c6b7ba3ddb510c7881f0eb051b9b1d70f8596abd653ad14213d

See more details on using hashes here.

File details

Details for the file venv_kernel-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: venv_kernel-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/22.5.0

File hashes

Hashes for venv_kernel-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 7688db61488fc479a0f5af36b50adb2732060e2fe4a2d4944cb7927573125251
MD5 96cf96dd4aac0e338bb60fbc7ef4a6bd
BLAKE2b-256 93a690a4fe8e25eca543f67dbede8721df59ac021c9b3f862c70ae83d33358de

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page