Python Jupyter kernel using pyproject environment manangers like Rye, PDM, Poetry, Hatch etc.
Project description
Pyproject Local Jupyter Kernel
- Use per-directory python projects to run Python Jupyter kernels
- Separate dependencies for notebooks in separate projects
- Use Rye, PDM, Poetry, Hatch, or similar project setups to define and run IPython kernels with dependencies for Jupyter notebooks.
Instead of installing a myriad of jupyter kernelspecs, one per project, instead have one "meta" kernel that enables the environment for the project the notebook file resides in. This approach should be more portable, usable to anyone who checks out your project structure from git, and easier to use.
Pyproject Local supports the following systems, and reads pyproject.toml to figure out which kind of project it is:
- Rye
- Poetry
- Hatch
- Pdm
- Uv
- Custom command (for other setups)
- Use venv at path (for other setups)
Quick Start
-
Install pyproject-local-kernel in your jupyterlab environment and restart jupyterlab
-
Create a new directory and notebook, select the Pyproject Local kernel for the notebook
-
Run (Example for Rye)
!rye init --virtual
!rye add --sync ipykernel
-
Restart the kernel and you are good to go. Use
!rye add
to add further dependencies.
- See the examples directory for how to setup jupyterlab and notebook projects separately. JupyterLab and the notebook are installed in separate environments.
User Experience
If the Pyproject Local kernel is used in a project where rye (or the relevant pyproject manager) is not installed, or the project does not have an ipykernel in the environment, then starting the kernel fails.
In that case a fallback kernel is started which that shows a message that it is not setup as expected in this environment. This is a regular ipython kernel which allows you to run shell commands and hopefully fix the configuration of the project.
It will give you some hints in the Jupyter notebook interface about the next steps to get it working. Example below is for Rye.
! Failed to start kernel! The detected project type is: Rye
! Is the virtual environment created, and does it have ipykernel in the project?
!
! Run this:
! !rye add --sync ipykernel
!
! Then restart the kernel to try again.
Configuration
Only one of the custom command and virtualenv path configurations can be used at a time.
Virtualenv Path
The key tool.pyproject-local-kernel.use-venv
can be a path to a virtualenv,
relative to the pyproject.toml file, which should be used.
[tool.pyproject-local-kernel]
use-venv = ".venv"
This is what's used automatically when Uv is detected (at the moment).
Custom Command
By default python from the local pyproject is run (using rye run, poetry run,
etc.). A custom command can be configured in pyproject.toml
- the pyproject
file closest to the notebook is used (and no other means of configuration are
supported).
The key tool.pyproject-local-kernel.python-cmd
should be a command that runs
python in the virtual environment you want to use for the project.
[tool.pyproject-local-kernel]
python-cmd = ["my", "custom", "python"]
Project Status
Status: Working proof of concept, published to PyPI. Additional interest and maintainer help is welcomed.
See also:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pyproject_local_kernel-0.6.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d90f8f902b3bcc58ad1f992bd8988c2d5d2a8b96bd5acc9bb2d4d0fe0e6fc69 |
|
MD5 | a7047ffd0f94d83fb5a38e4accccb7dd |
|
BLAKE2b-256 | a8cef58c9a2fcd6224e9a5ded36e71797eac5e678146ea8ff690aca467a45020 |
Hashes for pyproject_local_kernel-0.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b1b411a0f91f7185df96c65a67d6d9eb4e5be719e31fdd0912480389f93d965 |
|
MD5 | 53946c63a8a966e170406bdd4670f387 |
|
BLAKE2b-256 | 4feb0ad0f0b5919c3ff969cfd62c7be72ed6c59ff05d96c5586f564c8d992d1f |