Skip to main content

Jupyterlab extension to detect notebook kernels similarly to how nb_conda_kernel does

Project description

nb_venv_kernels

GitHub Actions npm version PyPI version Total PyPI downloads JupyterLab 4 Brought To You By KOLOMOLO Donate PayPal

Use Python virtual environments as Jupyter kernels. Discovers and registers kernels from venv, uv, and conda environments in JupyterLab's kernel selector.

This kernel spec manager is used by Copier Data Science - a modern Copier-based alternative to Cookiecutter Data Science with template updates and enhanced tooling. The template uses nb_venv_kernels to automatically manage Jupyter kernels for project environments.

For rich UI capabilities including kernel unregistration and virtual environment removal directly from JupyterLab, see jupyterlab_nb_venv_kernels_ui_extension.

Discovered environments appear in JupyterLab's kernel selector.

UV and Conda virtual environments co-exist and are properly discovered

CLI provides environment listing with status indicators.

CLI list command

Scan for environments from JupyterLab's Command Palette (Ctrl+Shift+C).

Command palette scan command

Results display in a modal with action indicators.

Scan results modal

Features

  • Unified kernel discovery - conda, venv, and uv environments in one kernel selector
  • Auto-detection - distinguishes uv from venv via pyvenv.cfg
  • Smart ordering - current environment first, then conda, uv, venv, system
  • Drop-in replacement - replaces nb_conda_kernels while preserving all conda functionality
  • Directory scanning - scan project directories to find and register environments
  • JupyterLab integration - scan for environments from Command Palette with results modal
  • CLI management - register, unregister, scan, list with color-coded status indicators
  • Programmatic API - REST endpoints and Python API for automation (--json flag)
  • Zero config - auto-enables on install, works immediately

Built because someone got tired of typing python -m ipykernel install --user --name=... for the 42nd time.

Install

This package should be installed in the environment from which you run Jupyter Notebook or JupyterLab. This might be your base conda environment, but it need not be. For instance, if you have a dedicated jupyter_env environment:

conda activate jupyter_env
pip install nb_venv_kernels

Or install directly with pip/uv if not using conda:

pip install nb_venv_kernels

The extension auto-enables as the default kernel spec manager via jupyter_config.json. If nb_conda_kernels is installed, nb_venv_kernels takes precedence and includes all conda kernel discovery functionality.

Usage

Virtual environments require ipykernel installed to be discoverable. The ipykernel package creates the kernel spec at {env}/share/jupyter/kernels/*/kernel.json.

# In your virtual environment
pip install ipykernel

Scanning for Environments

Scan a directory tree to find and register all venv/uv environments:

nb_venv_kernels scan                      # Scan current directory
nb_venv_kernels scan /path/to/projects    # Scan specific directory
nb_venv_kernels scan --depth 3            # Limit recursion depth (default: 5)

The scan command automatically:

  • Finds venv and uv environments by detecting bin/python
  • Registers them in the appropriate registry (~/.venv/ or ~/.uv/)
  • Cleans up non-existent environments from registries
  • Reports conda environments found (they are discovered via conda env list)

Manual Registration

nb_venv_kernels register /path/to/.venv
nb_venv_kernels register /path/to/.venv -n "My Project"  # With custom display name
nb_venv_kernels unregister /path/to/.venv
nb_venv_kernels unregister -n "My Project"               # Unregister by name

Custom names are stored in the registry and used in kernel display names (venv/uv only, ignored for conda). Re-registering an existing environment with a different name updates it.

Listing Environments

The list command shows all environments (conda, uv, venv) with their status:

nb_venv_kernels list
name                      type             exists   kernel   path
--------------------------------------------------------------------------------------------------------------
base                      conda            yes      yes      /opt/conda
my-conda-env              conda (local)    yes      yes      /opt/conda/envs/my-conda-env
my-project                uv               yes      yes      /home/user/my-project/.venv
another-project           venv             yes      no       /home/user/another-project/venv

Environments are sorted by type (conda, uv, venv) then by name. The KERNEL column indicates whether ipykernel is installed. Missing environments and kernels display "no" in red. Duplicate names are automatically resolved with _1, _2 suffixes.

Jupyter Configuration

nb_venv_kernels config enable     # Enable VEnvKernelSpecManager
nb_venv_kernels config disable    # Disable VEnvKernelSpecManager
nb_venv_kernels config show       # Show current config status

Registered environments with ipykernel appear in JupyterLab's kernel selector.

Environment Registries

Environments are registered in separate files based on their source:

  • venv: ~/.venv/environments.txt
  • uv: ~/.uv/environments.txt
  • conda: ~/.conda/environments.txt + global environments from conda env list

Registry format is plain text with optional tab-separated custom names:

/path/to/env
/path/to/other/env	custom-name

The register command auto-detects uv environments via pyvenv.cfg and writes to the appropriate registry.

JupyterLab Commands

The extension adds commands to JupyterLab accessible via Command Palette (Ctrl+Shift+C):

Command Command ID Description
Scan for Virtual Environments nb_venv_kernels:scan Scans workspace for venv/uv/conda environments and registers them. Results displayed in modal with action indicators
Refresh Kernel List nb_venv_kernels:refresh Immediately refreshes the kernel selector. Use after CLI register/unregister operations

The kernel list is cached for 60 seconds (same as nb_conda_kernels). Use "Refresh Kernel List" for immediate updates after CLI changes.

How It Works

Environment registration and kernel discovery flow:

flowchart LR
    subgraph CLI
        SCAN[nb_venv_kernels scan]
        REG[nb_venv_kernels register]
    end

    subgraph Registries
        VENV[~/.venv/environments.txt]
        UV[~/.uv/environments.txt]
        CONDA_REG[~/.conda/environments.txt]
    end

    subgraph Auto-Discovery
        CONDA_ENV[conda env list]
        PYVENV[pyvenv.cfg]
    end

    subgraph Discovery
        KSPM[VEnvKernelSpecManager]
        CKSPM[CondaKernelSpecManager]
    end

    SCAN -->|finds envs| VENV
    SCAN -->|finds envs| UV
    REG -->|registers| UV
    PYVENV -.->|detects uv| UV
    VENV --> KSPM
    UV --> KSPM
    CONDA_REG --> CKSPM
    CONDA_ENV --> CKSPM
    CKSPM --> KSPM
    KSPM --> JL[JupyterLab Kernel Selector]

    style SCAN stroke:#10b981,stroke-width:2px
    style REG stroke:#10b981,stroke-width:2px
    style VENV stroke:#10b981,stroke-width:2px
    style UV stroke:#a855f7,stroke-width:2px
    style CONDA_REG stroke:#f59e0b,stroke-width:2px
    style CONDA_ENV stroke:#f59e0b,stroke-width:2px
    style PYVENV stroke:#6b7280,stroke-width:2px
    style KSPM stroke:#3b82f6,stroke-width:3px
    style CKSPM stroke:#f59e0b,stroke-width:2px
    style JL stroke:#0284c7,stroke-width:3px

CLI and JupyterLab interface architecture:

flowchart LR
    subgraph Interfaces
        CLI[nb_venv_kernels CLI]
        PAL[JupyterLab Command Palette]
    end

    subgraph Server[Jupyter Server]
        REST[REST Endpoints<br>/nb-venv-kernels/*]
    end

    MGR[VEnvKernelSpecManager]

    subgraph Storage
        VENV_REG[~/.venv/environments.txt]
        UV_REG[~/.uv/environments.txt]
    end

    CLI -->|direct| MGR
    PAL -->|api| REST
    REST --> MGR
    MGR --> VENV_REG
    MGR --> UV_REG

    style CLI stroke:#10b981,stroke-width:2px
    style PAL stroke:#0284c7,stroke-width:2px
    style REST stroke:#f59e0b,stroke-width:2px
    style MGR stroke:#3b82f6,stroke-width:3px
    style VENV_REG stroke:#10b981,stroke-width:2px
    style UV_REG stroke:#a855f7,stroke-width:2px
  • Scans {path}/share/jupyter/kernels/*/kernel.json for each registered environment
  • Configures kernel to use venv's python directly with VIRTUAL_ENV and PATH environment variables
  • Kernel order: current environment first, then conda, uv, venv, system
  • Caches results for 60 seconds
  • config enable backs up existing config, config disable restores from backup
  • CLI calls VEnvKernelSpecManager directly; JupyterLab frontend uses REST endpoints

For detailed technical documentation, see Environment Discovery Mechanism.

Configuration

Optional settings in jupyter_server_config.py:

c.VEnvKernelSpecManager.venv_only = True                      # Hide system/conda kernels
c.VEnvKernelSpecManager.env_filter = r"\.tox|\.nox"           # Exclude by pattern
c.VEnvKernelSpecManager.name_format = "{language} [{source} env:{environment}]"  # Default format
c.VEnvKernelSpecManager.scan_depth = 10                       # Default depth for scan command
c.VEnvKernelSpecManager.require_kernelspec = True             # Only register envs with ipykernel

Display name variables: {language}, {environment}, {source} (uv/venv), {kernel}, {display_name}

Programmatic API

The extension provides REST and Python APIs for integration with tools and automation. See API Documentation for details on:

  • REST endpoints - /nb-venv-kernels/environments, /scan, /register, /unregister, /refresh
  • Python API - VEnvKernelSpecManager methods for listing, scanning, and registering environments
  • JSON output - CLI commands support --json flag for machine-to-machine communication

Uninstall

pip uninstall nb_venv_kernels

After uninstall, nb_conda_kernels (if installed) will resume handling kernel discovery.

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

nb_venv_kernels-1.2.36.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nb_venv_kernels-1.2.36-py3-none-any.whl (61.6 kB view details)

Uploaded Python 3

File details

Details for the file nb_venv_kernels-1.2.36.tar.gz.

File metadata

  • Download URL: nb_venv_kernels-1.2.36.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for nb_venv_kernels-1.2.36.tar.gz
Algorithm Hash digest
SHA256 ca199d518f6bcf4baf51575625c2b5e32048a9f283c128bdc5ff290be2794137
MD5 dbae7f405ba9642d42c11e0a6d89155d
BLAKE2b-256 9a08c479654f46757a21964a3dd195a01c97a904ebe66f2ccd3e97513b2e31f2

See more details on using hashes here.

File details

Details for the file nb_venv_kernels-1.2.36-py3-none-any.whl.

File metadata

File hashes

Hashes for nb_venv_kernels-1.2.36-py3-none-any.whl
Algorithm Hash digest
SHA256 fd37c7bdbc88a30760bdd9db28dc194b879c0e83249bcb11e2725c2903fa7643
MD5 be910ed7c455ec5b67f2511aa2a74f3c
BLAKE2b-256 a4e08e4ea56b0465a7ae334fbc605f94c15569540b14c02e8b842d207198798f

See more details on using hashes here.

Supported by

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