support

probe and install what a kernel needs into the environment it will run in

Check and install kernel support in a selected Python environment.

KERNEL_PACKAGES lists the required distributions. as_installed adds versions from the host environment.

HOST_PY, INSPECTOR, KERNEL_PACKAGES
((3, 11),
 ('dhrishti',),
 ('ipykernel', 'ipymini', 'kernmini', 'fastcore', 'microio'))

source

work_dir

def work_dir(
    cwd:NoneType=None
):

cwd as a string, falling back to a writable directory when the process inherited / or a read-only bundle.

work_dir returns a supplied directory as text. A missing directory uses the current working directory.

work_dir(Path('/repo/notebooks'))
'/repo/notebooks'

source

support_paths

def support_paths():

The sys.path entries bootstrap_src hands a kernel to borrow from.

support_paths returns existing absolute import paths. Relative and missing paths are excluded.

Each interpreter path has one shared probe lock.

test_is(_lock_for('/a/.venv/bin/python'), _lock_for('/a/.venv/bin/python'))
assert _lock_for('/a/.venv/bin/python') is not _lock_for('/b/.venv/bin/python')

_run always returns returncode, stdout, and stderr. Process-start failures become a non-zero result.

p = _run(['/definitely/not/a/python', '-c', 'pass'])
p.returncode, p.stderr
(127, "[Errno 2] No such file or directory: '/definitely/not/a/python'")

_last_line returns the last non-empty line, or an empty string.

_last_line('Traceback (most recent call last):\n  File "<string>", line 1\nImportError: numpy ABI\n\n')
'ImportError: numpy ABI'

A module probe returns available, version, and error.

tmp = TemporaryDirectory(); d = Path(tmp.name)
(d/'borrowed.py').write_text('__version__ = "1.2"')
_probe(sys.executable, 'borrowed', paths=[str(d)])
{'available': True, 'version': '1.2', 'error': ''}

source

kernel_support

def kernel_support(
    python
):

Importability of both supported kernel launchers in python.

kernel_support probes both kernel launchers in the selected interpreter.

ok = kernel_support(sys.executable)['ipymini']
gone = kernel_support('/definitely/not/a/python')['ipymini']
ok, gone
({'available': True, 'version': '0.1.21', 'error': ''},
 {'available': False,
  'version': '',
  'error': "[Errno 2] No such file or directory: '/definitely/not/a/python'"})

source

import_failure

def import_failure(
    err
):

Whether bootstrap failed on an inspector import.


source

inspector_support

def inspector_support(
    python, refresh:bool=False
):

Check whether python can import the live-variable inspector, returning what blocked it if not.

inspector_support caches one result per interpreter for INSPECTOR_TTL seconds. import_failure recognizes a missing or broken inspector import.

inspector_support(sys.executable), import_failure("ModuleNotFoundError: No module named 'dhrishti'")
{'available': False,
 'version': '',
 'error': "ModuleNotFoundError: No module named 'dhrishti'"}

source

installable

def installable(
    python
):

Whether anything may install into python. Never this one, and never inside a signed bundle.

installable rejects a missing path, the current interpreter, and interpreters inside a macOS application bundle.

installable(sys.executable), installable('/repo/.venv/bin/python')
(False, True)

source

as_installed

def as_installed(
    names:tuple=('ipykernel', 'ipymini', 'kernmini', 'fastcore', 'microio')
):

Pin names to the versions this process runs; leave unpinned what it doesn’t have, so installs stay compatible with the host.

as_installed pins names to versions installed in the host process.

as_installed(['fastcore', 'ipymini', 'not_a_real_package'])
['fastcore==2.2.19', 'ipymini==0.1.21', 'not_a_real_package']

_attempts tries uv when available and always includes python -m pip install.

_attempts('/nowhere/bin/python', ['ipymini==0.1.21'])[-1]
['/nowhere/bin/python', '-m', 'pip', 'install', 'ipymini==0.1.21']

_log records the command, return code, and bounded output.


source

install_kernel_support

def install_kernel_support(
    python, packages:NoneType=None
):

Install kernel packages via uv or pip; verify the import works after, so failures name the package not the installer.

install_kernel_support succeeds only when installation completes and both launchers import.


source

install_inspector_support

def install_inspector_support(
    python, packages:tuple=('dhrishti',)
):

Give python its own copy of what the inspector imports, then ask it again.

install_inspector_support installs Dhrishti and probes it again.