Define custom kernels (notebook)¶
uv managed virtual environment¶
We can use uv to create a virtual environment for different versions of Python.
Let's create a new virtual environment in the folder named ".venv_py315" using uv.
Requires a recent version of uv to be installed.
In [1]:
Copied!
%uv venv .venv_py315 --python 3.15 --clear
%uv pip install --directory .venv_py315 --python 3.15 async-kernel
%uv venv .venv_py315 --python 3.15 --clear
%uv pip install --directory .venv_py315 --python 3.15 async-kernel
Downloading cpython-3.15.0a5-linux-x86_64-gnu (download) (35.1MiB)
Now we can write a kernel spec that uses uv to start the kernel from the virtual environment.
In [2]:
Copied!
import pathlib
from async_kernel.kernelspec import write_kernel_spec
uv_path = pathlib.Path.cwd().joinpath(".venv_py315")
assert uv_path.exists()
write_kernel_spec(
kernel_name="async_3.15",
display_name="Python 3.15 (async)",
env={"UV_PROJECT_ENVIRONMENT": str(uv_path)},
executable=("uv", "run", "--no-sync", "python", "-m", "async_kernel"),
)
import pathlib
from async_kernel.kernelspec import write_kernel_spec
uv_path = pathlib.Path.cwd().joinpath(".venv_py315")
assert uv_path.exists()
write_kernel_spec(
kernel_name="async_3.15",
display_name="Python 3.15 (async)",
env={"UV_PROJECT_ENVIRONMENT": str(uv_path)},
executable=("uv", "run", "--no-sync", "python", "-m", "async_kernel"),
)
Out[2]:
PosixPath('/home/runner/work/async-kernel/async-kernel/.venv/share/jupyter/kernels/async_3.15')
The kernel spec with the display name "Python 3.15 (async kernel)" has been added. You will need to refresh the list of kernels for it to be available.