Metadata-Version: 2.3
Name: pyproject-local-kernel
Version: 0.10.0
Summary: Python Jupyter kernel using project/environment manangers like Rye, Uv, PDM, Poetry, Hatch etc.
Project-URL: Homepage, https://bluss.github.io/pyproject-local-kernel/
Project-URL: Repository, https://github.com/bluss/pyproject-local-kernel
Author-email: Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
License: # LICENSE
        ## For pyproject_local_kernel
        
        In general, unless otherwise noted below.
        
        Copyright 2023-2024 Ulrik Sverdrup "bluss"
        
        Copyright 2021 Pathbird Inc
        
        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.
        
        ## For ./tests
        
        Copyright 2023-2024 Ulrik Sverdrup "bluss"
        
        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.
        
        ## For file ./src/pyproject_local_kernel/parentpoller.py
        
        BSD 3-Clause License
        
        Copyright (c) 2024, Ulrik Sverdrup "bluss"
        Copyright (c) 2015, IPython Development Team
        
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License-File: LICENSE.md
Keywords: Interactive,Interpreter,Jupyter,Pyproject
Classifier: Framework :: Jupyter
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: BSD License
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Requires-Dist: jupyter-client>=8.6.3
Requires-Dist: tomli>=2.0.1; python_version < '3.11'
Description-Content-Type: text/markdown


# Pyproject Local Jupyter Kernel

<p class="web_hidden">

[Website][] - [PyPI][] [![PyPI - Python Version](https://img.shields.io/pypi/v/pyproject-local-kernel)][PyPi]

</p>

Separate dependencies for Jupyter notebooks in separate projects.

Use python project managers to define dependencies:
  use one of Uv, Rye, PDM, Poetry, Hatch (and so on).

[Website]: https://bluss.github.io/pyproject-local-kernel/
[PyPi]: https://pypi.org/project/pyproject-local-kernel/

Instead of installing a myriad of jupyter kernelspecs, one per project, with
this solution there is only 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:

Uv <br>
Rye <br>
Poetry <br>
Hatch <br>
PDM <br>

A custom command or direct use of virtual environment can also be configured.

## Quick Start (JupyterLab)

1. Install pyproject-local-kernel in your jupyterlab environment and restart
   jupyterlab
2. Create a new directory and notebook
3. Select the **Pyproject Local** kernel for the notebook
4. Run these shell commands in the notebook to setup the new project:<br>
   
  (Example for Uv:)

  `!uv init && uv add ipykernel`

  (Example for Rye:)

  `!rye init --virtual && rye add ipykernel`


Now restart the kernel and you are good to go. Use more `add` commands 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.


Do you want to use pyproject-local-kernel in other environments, like
**VSCodium or VS Code**, or maybe using Pipenv? See our [FAQ][] for more
information.

[FAQ]: FAQ.md

## User Experience

If the Pyproject Local kernel is used in a project where Uv (or the relevant
project 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.

```diff
! 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

Configuration is optional and is read from `pyproject.toml`. Only the
`pyproject.toml` closest to the notebook is read. Defaults are based on
“sniffing” the `pyproject.toml` to detect which project manager is in use.

### `python-cmd`

The key `tool.pyproject-local-kernel.python-cmd` should be a command that runs
python from the environment you want to use for the project.

If this is set then it overrides the default command.

**Default:** *Depends on project manager*<br>
**Type:** `list[str] | str`<br>
**Example:**

```toml
[tool.pyproject-local-kernel]
python-cmd = ["uv", "run", "--with", "ipykernel", "python"]
```

### `use-venv`

Path to virtual environment that should be used, relative to the
`pyproject.toml` file. Can also be an absolute path.

If this is set then it overrides the default command.

**Default:** Not set<br>
**Type:** `str`<br>
**Example:**

```toml
[tool.pyproject-local-kernel]
use-venv = ".venv"
```


## About Particular Project Managers

The project manager command, be it rye, uv, pdm, etc needs to be
available on the path where jupyterlab runs. Either install the project
manager in the jupyterlab environment, or install the project manager
user-wide (using something like pipx, rye tools, uv tool, brew, or
other method to install it.)

### Rye

- Rye is detected if the pyproject.toml contains `tool.rye.managed = true`
  which Rye sets by default for its new projects.

### Uv

- Uv is detected if the pyproject.toml contains `tool.uv`. It is also the
  default fallback if no project manager is detected from a pyproject file.

- The command used is `uv run --with ipykernel python` which means that it ensures
  `ipykernel` is used even if it's not already in the project(!). However, note that
  it uses an ephemeral virtual environment for ipykernel in that case. Add
  ipykernel to the project to avoid this.

### PDM

- PDM is detected if pyproject.toml contains `tool.pdm`

### Hatch

- Hatch is detected if pyproject.toml contains `tool.hatch.envs`

- By default it calls out to `hatch env find`, to find the default virtualenv,
  and runs from there. `hatch run` should not be used directly because
  it's not compatible with how kernel interrupts work (as of this writing).

- It's best to create the hatch project, add ipykernel as dependency and sync
  dependencies in a terminal before starting (it does not work so well with
  shell commands in a notebook).

## Project Status

Additional interest and maintainer help is welcomed.

## Links

* <https://github.com/mitsuhiko/rye>
* <https://github.com/astral-sh/uv>
* <https://github.com/goerz/python-localvenv-kernel>
* <https://github.com/pathbird/poetry-kernel>

## License

`pyproject-local-kernel` is open source. See the LICENSE.md file in the source
distribution for more information.
