Python Jupyter kernel using Poetry for reproducible notebooks

Overview

Poetry Kernel

Use per-directory Poetry environments to run Jupyter kernels. No need to install a Jupyter kernel per Python virtual environment!

The idea behind this project is to allow you to capture the exact state of your environment. This means you can email your work to your peers, and they'll have exactly the same set of packages that you do! Reproducibility!

Why not virtual environments (venvs)?

Virtual environments were (and are) an important advancement to Python's package management story, but they have a few shortcomings:

  • They are not great for reproducibility. Usually, you'll create a new virtual environment using a requirements.txt which includes all the direct dependencies (numpy, pandas, etc.), but not transient dependencies (pandas depends on pytz for timezone support, for example). And usually, even the direct dependencies are specified only as minimum (or semver) ranges (e.g., numpy>=1.21) which can make it hard or impossible to accurately recreate the venv later.
  • With Jupyter, they usually require that the kernels be installed globally. This means you'll need need to have a separate kernelspec for every venv you want to use with Jupyter.

Poetry uses venvs transparently under the hood by constructing them from the pyproject.toml and poetry.lock files. The poetry.lock file records the exact state of dependencies (and transient dependencies) and can be used to more accurately reproduce the environment.

Additionally, Poetry Kernel means you only have to install one kernelspec. It then uses the pyproject.toml file from the directory of the notebook (or any parent directory) to choose which environment to run the notebook in.

Shameless plug

The reason we created this package was to make sure that the code environments created for running student code on Pathbird exactly match your development environment. Interested in developing interactive, engaging, inquiry-based lessons for your students? Check out Pathbird for more information!

Usage

  1. Install Poetry if not yet installed.
  2. Install this package:
    # NOTE: Do **NOT** install this package in your Poetry project, it should be
    # installed at the system or user level.
    pip3 install --user poetry-kernel
  3. Initialize a Poetry project (only required if you do not have an existing Poetry project ready to use):
    poetry init -n
  4. IMPORTANT: Add ipykernel to your project's dependencies:
    # In the directory of your Poetry project
    poetry add ipykernel
  5. Start a "Poetry" Jupyter kernel and see it in action! Jupyter launcher screenshot

Troubleshooting

Kernel isn't starting ("No Kernel" message)

Pro-tip: Check the output of the terminal window where you launched Jupyter. It will usually explain why the kernel is failing to start.

  1. Make sure that you are launching a notebook in a directory/folder that contains a Poetry project (pyproject.toml and poetry.lock files). You can turn a directory into a Poetry project by running:
poetry init -n
  1. Make sure that you've installed ipykernel into your project:
poetry add ipykernel
  1. Make sure the Poetry project is installed! This is especially important for projects that you have downloaded from others (warning: installing a Poetry project could run arbitrary code on your computer, make sure you trust your download first!):

    poetry install
  2. Still can't figure it out? Open an issue!

A package I added won't import properly

If you added the package after starting the kernel, you might need to restart the kernel for it to see the new package.

FAQ

See FAQ.md.

Comments
  • Windows Does not have SIGKILL

    Windows Does not have SIGKILL

    I believe the following line needs more attention to be compatible with Windows considering windows does not have SIGKILL: https://github.com/pathbird/poetry-kernel/blob/main/poetry_kernel/main.py#L39

    bug good first issue prs accepted 
    opened by amirhessam88 4
  • FileNotFoundError: [Errno 2] No such file or directory: 'poetry'.

    FileNotFoundError: [Errno 2] No such file or directory: 'poetry'.

    Hey, cool project!

    I got the following error, not sure how informative it is for you. Happy to dig into logs etc. I'm using a Gitpod workspace with a public github repo too, so could potentially offer a completely reproducible clean environment.

    Failed to start the Kernel. 
    FileNotFoundError: [Errno 2] No such file or directory: 'poetry'. 
    
    opened by andrewcstewart 3
  • Check in parent_dirs for pyproject.toml

    Check in parent_dirs for pyproject.toml

    This now loops through parent directories to check for pyproject.toml to allow for kernel execution when running notebooks in sub directories. I used the same code from poetry itself to ensure compatibility

    opened by nick-gorse 3
  • modified forward_signals to be compatible with windows

    modified forward_signals to be compatible with windows

    in regard to issue #3

    removed signal.SIGTERM from forward_signals, as well as two others as they could not be used by signal.signal (ValueError: invalid signal value)

    from https://docs.python.org/3/library/signal.html signal.CTRL_C_EVENT The signal corresponding to the Ctrl+C keystroke event. This signal can only be used with os.kill()

    same for signal.CTRL_BREAK_EVENT

    opened by gpfv 0
  • Check in parent_dirs for pyproject.toml

    Check in parent_dirs for pyproject.toml

    This now loops through parent directories to check for pyproject.toml to allow for kernel execution when running notebooks in sub directories. I used the same code as used poetry itself to ensure compatibility

    opened by nick-gorse 0
  • poetry-kernel uses Jupyter env instead of notebook env

    poetry-kernel uses Jupyter env instead of notebook env

    If a poetry pyproject.toml is used to create the environment for the Jupyter lab (adding poetry-kernel as a dependency), then no matter in which working directory the jupyter server is started, any notebook that is opened with the "poetry" kernel will have the Jupyter environment, not the environment of the notebook's project.

    Minimal example: https://github.com/drakesiardxy/poetry-kernel-bug To replicate: Create the jupyer-base env and the kernel_a env separately, then start the jupyter server with the first environment and attempt to run kernel_a.ipynb using the "Poetry" kernel. pandas will be missing, because the notebook will have been launched with the environment of the server, not the environment belonging to the notebook's project.

    opened by drakesiardxy 0
  • Is it possible to make poetry-kernel work with JupyterHub?

    Is it possible to make poetry-kernel work with JupyterHub?

    Hi!

    We have a multi-user JupyterHub instance and we would like to use your library. So far we have only been able to see the poetry-kernel button in the kernel selection screen by installing poetry kernel at the user level (if we install poetry kernel from the user that launches JupyterHub, the root user, the button does not show). But the problem that we have is that when we press the poetry button and create a new notebook in a folder with a poetry project (and ipykernel installed), the kernel is never connected so no code can be executed. The generated logs are the following:

    Apr 26 15:30:26 labs-ubuntu-20-04 python3[446728]: [I 2022-04-26 15:30:26.298 SingleUserLabApp restarter:66] AsyncIOLoopKernelRestarter: restarting kernel (2/5), new random ports
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]: Traceback (most recent call last):
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:   File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:     return _run_code(code, main_globals, None,
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:   File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:     exec(code, run_globals)
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:   File "/home/marta/.local/lib/python3.8/site-packages/poetry_kernel/__main__.py", line 68, in <module>
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:     main()
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:   File "/home/marta/.local/lib/python3.8/site-packages/poetry_kernel/__main__.py", line 37, in main
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:     proc = subprocess.Popen(cmd)
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:   File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:     self._execute_child(args, executable, preexec_fn, close_fds,
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:   File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]:     raise child_exception_type(errno_num, err_msg, err_filename)
    Apr 26 15:30:26 labs-ubuntu-20-04 python3[1028846]: FileNotFoundError: [Errno 2] No such file or directory: 'poetry'
    

    This looks like poetry is not being found by the library although it is indeed installed both at the root and user level. Is there a way to solve this? Or is this case out of the scope of the library for now?

    opened by MsLimon 2
Releases(v0.1.2)
  • v0.1.2(Mar 30, 2022)

    What's Changed

    • modified forward_signals to be compatible with windows by @gpfv in https://github.com/pathbird/poetry-kernel/pull/4

    New Contributors

    • @gpfv made their first contribution in https://github.com/pathbird/poetry-kernel/pull/4

    Full Changelog: https://github.com/pathbird/poetry-kernel/compare/v0.1.1...v0.1.2

    Source code(tar.gz)
    Source code(zip)
Owner
Pathbird
Pathbird is a platform for instructors to build interactive, engaging, inquiry-based lessons for computational courses.
Pathbird
Think Big, Teach Small: Do Language Models Distil Occam’s Razor?

Think Big, Teach Small: Do Language Models Distil Occam’s Razor? Software related to the paper "Think Big, Teach Small: Do Language Models Distil Occa

0 Dec 07, 2021
Official Repo for ICCV2021 Paper: Learning to Regress Bodies from Images using Differentiable Semantic Rendering

[ICCV2021] Learning to Regress Bodies from Images using Differentiable Semantic Rendering Getting Started DSR has been implemented and tested on Ubunt

Sai Kumar Dwivedi 83 Nov 27, 2022
Point-NeRF: Point-based Neural Radiance Fields

Point-NeRF: Point-based Neural Radiance Fields Project Sites | Paper | Primary c

Qiangeng Xu 662 Jan 01, 2023
Towards Ultra-Resolution Neural Style Transfer via Thumbnail Instance Normalization

Towards Ultra-Resolution Neural Style Transfer via Thumbnail Instance Normalization Official PyTorch implementation for our URST (Ultra-Resolution Sty

czczup 148 Dec 27, 2022
Official Implementation for "ReStyle: A Residual-Based StyleGAN Encoder via Iterative Refinement" https://arxiv.org/abs/2104.02699

ReStyle: A Residual-Based StyleGAN Encoder via Iterative Refinement Recently, the power of unconditional image synthesis has significantly advanced th

967 Jan 04, 2023
🔥3D-RecGAN in Tensorflow (ICCV Workshops 2017)

3D Object Reconstruction from a Single Depth View with Adversarial Learning Bo Yang, Hongkai Wen, Sen Wang, Ronald Clark, Andrew Markham, Niki Trigoni

Bo Yang 125 Nov 26, 2022
WHENet - ONNX, OpenVINO, TFLite, TensorRT, EdgeTPU, CoreML, TFJS, YOLOv4/YOLOv4-tiny-3L

HeadPoseEstimation-WHENet-yolov4-onnx-openvino ONNX, OpenVINO, TFLite, TensorRT, EdgeTPU, CoreML, TFJS, YOLOv4/YOLOv4-tiny-3L 1. Usage $ git clone htt

Katsuya Hyodo 49 Sep 21, 2022
3D-printable hand-strapped keyboard

Note: This repo has not been cleaned up and prepared for general consumption at all. This is just a dump of the project files. If there is any interes

Wojciech Baranowski 41 Dec 31, 2022
Public Models considered for emotion estimation from EEG

Emotion-EEG Set of models for emotion estimation from EEG. Composed by the combination of two deep-learing models learning together (RNN and CNN) with

Victor Delvigne 21 Dec 23, 2022
A DeepStack custom model for detecting common objects in dark/night images and videos.

DeepStack_ExDark This repository provides a custom DeepStack model that has been trained and can be used for creating a new object detection API for d

MOSES OLAFENWA 98 Dec 24, 2022
An implementation of Video Frame Interpolation via Adaptive Separable Convolution using PyTorch

This work has now been superseded by: https://github.com/sniklaus/revisiting-sepconv sepconv-slomo This is a reference implementation of Video Frame I

Simon Niklaus 984 Dec 16, 2022
Ranger - a synergistic optimizer using RAdam (Rectified Adam), Gradient Centralization and LookAhead in one codebase

Ranger-Deep-Learning-Optimizer Ranger - a synergistic optimizer combining RAdam (Rectified Adam) and LookAhead, and now GC (gradient centralization) i

Less Wright 1.1k Dec 21, 2022
Changing the Mind of Transformers for Topically-Controllable Language Generation

We will first introduce the how to run the IPython notebook demo by downloading our pretrained models. Then, we will introduce how to run our training and evaluation code.

IESL 20 Dec 06, 2022
Towards uncontrained hand-object reconstruction from RGB videos

Towards uncontrained hand-object reconstruction from RGB videos Yana Hasson, Gül Varol, Ivan Laptev and Cordelia Schmid Project page Paper Table of Co

Yana 69 Dec 27, 2022
Normalization Calibration (NorCal) for Long-Tailed Object Detection and Instance Segmentation

NorCal Normalization Calibration (NorCal) for Long-Tailed Object Detection and Instance Segmentation On Model Calibration for Long-Tailed Object Detec

Tai-Yu (Daniel) Pan 24 Dec 25, 2022
In Search of Probeable Generalization Measures

In Search of Probeable Generalization Measures Exciting News! In Search of Probeable Generalization Measures has been accepted to the International Co

Mahdi S. Hosseini 6 Sep 11, 2022
MPRNet-Cloud-removal: Progressive cloud removal

MPRNet-Cloud-removal Progressive cloud removal Requirements 1.Pytorch = 1.0 2.Python 3 3.NVIDIA GPU + CUDA 9.0 4.Tensorboard Installation 1.Clone the

Semi 95 Dec 18, 2022
Robust Instance Segmentation through Reasoning about Multi-Object Occlusion [CVPR 2021]

Robust Instance Segmentation through Reasoning about Multi-Object Occlusion [CVPR 2021] Abstract Analyzing complex scenes with DNN is a challenging ta

Irene Yuan 24 Jun 27, 2022
A PyTorch implementation of "From Two to One: A New Scene Text Recognizer with Visual Language Modeling Network" (ICCV2021)

From Two to One: A New Scene Text Recognizer with Visual Language Modeling Network The official code of VisionLAN (ICCV2021). VisionLAN successfully a

81 Dec 12, 2022
This is the code of NeurIPS'21 paper "Towards Enabling Meta-Learning from Target Models".

ST This is the code of NeurIPS 2021 paper "Towards Enabling Meta-Learning from Target Models". If you use any content of this repo for your work, plea

Su Lu 7 Dec 06, 2022