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
A Multi-modal Perception Tracker (MPT) for speaker tracking using both audio and visual modalities

MPT A Multi-modal Perception Tracker (MPT) for speaker tracking using both audio and visual modalities. Implementation for our AAAI 2022 paper: Multi-

yidiLi 4 May 08, 2022
SuRE Evaluation: A Supplementary Material

SuRE Evaluation: A Supplementary Material This repository contains supplementary material regarding the evaluations presented in the paper Visual Expl

NYU Visualization Lab 0 Dec 14, 2021
The official implementation of the IEEE S&P`22 paper "SoK: How Robust is Deep Neural Network Image Classification Watermarking".

Watermark-Robustness-Toolbox - Official PyTorch Implementation This repository contains the official PyTorch implementation of the following paper to

49 Dec 19, 2022
Code and data for "Broaden the Vision: Geo-Diverse Visual Commonsense Reasoning" (EMNLP 2021).

GD-VCR Code for Broaden the Vision: Geo-Diverse Visual Commonsense Reasoning (EMNLP 2021). Research Questions and Aims: How well can a model perform o

Da Yin 24 Oct 13, 2022
Dynamic Bottleneck for Robust Self-Supervised Exploration

Dynamic Bottleneck Introduction This is a TensorFlow based implementation for our paper on "Dynamic Bottleneck for Robust Self-Supervised Exploration"

Bai Chenjia 4 Nov 14, 2022
Python lib to talk to pylontech lithium batteries (US2000, US3000, ...) using RS485

python-pylontech Python lib to talk to pylontech lithium batteries (US2000, US3000, ...) using RS485 What is this lib ? This lib is meant to talk to P

Frank 26 Dec 28, 2022
Python implementation of 3D facial mesh exaggeration using the techniques described in the paper: Computational Caricaturization of Surfaces.

Python implementation of 3D facial mesh exaggeration using the techniques described in the paper: Computational Caricaturization of Surfaces.

Wonjong Jang 8 Nov 01, 2022
Code for NeurIPS2021 submission "A Surrogate Objective Framework for Prediction+Programming with Soft Constraints"

This repository is the code for NeurIPS 2021 submission "A Surrogate Objective Framework for Prediction+Programming with Soft Constraints". Edit 2021/

10 Dec 20, 2022
Official code of ICCV2021 paper "Residual Attention: A Simple but Effective Method for Multi-Label Recognition"

CSRA This is the official code of ICCV 2021 paper: Residual Attention: A Simple But Effective Method for Multi-Label Recoginition Demo, Train and Vali

163 Dec 22, 2022
Benchmarks for semi-supervised domain generalization.

Semi-Supervised Domain Generalization This code is the official implementation of the following paper: Semi-Supervised Domain Generalization with Stoc

Kaiyang 49 Dec 10, 2022
This repository contains the implementation of the paper: "Towards Frequency-Based Explanation for Robust CNN"

RobustFreqCNN About This repository contains the implementation of the paper "Towards Frequency-Based Explanation for Robust CNN" arxiv. It primarly d

Sarosij Bose 2 Jan 23, 2022
Writeups for the challenges from DownUnderCTF 2021

cloud Challenge Author Difficulty Release Round Bad Bucket Blue Alder easy round 1 Not as Bad Bucket Blue Alder easy round 1 Lost n Found Blue Alder m

DownUnderCTF 161 Dec 31, 2022
Python scripts for performing lane detection using the LSTR model in ONNX

ONNX LSTR Lane Detection Python scripts for performing lane detection using the Lane Shape Prediction with Transformers (LSTR) model in ONNX. Requirem

Ibai Gorordo 29 Aug 30, 2022
This project provides an unsupervised framework for mining and tagging quality phrases on text corpora with pretrained language models (KDD'21).

UCPhrase: Unsupervised Context-aware Quality Phrase Tagging To appear on KDD'21...[pdf] This project provides an unsupervised framework for mining and

Xiaotao Gu 146 Dec 22, 2022
Expressive Body Capture: 3D Hands, Face, and Body from a Single Image

Expressive Body Capture: 3D Hands, Face, and Body from a Single Image [Project Page] [Paper] [Supp. Mat.] Table of Contents License Description Fittin

Vassilis Choutas 1.3k Jan 07, 2023
Code repo for "Transformer on a Diet" paper

Transformer on a Diet Reference: C Wang, Z Ye, A Zhang, Z Zhang, A Smola. "Transformer on a Diet". arXiv preprint arXiv (2020). Installation pip insta

cgraywang 31 Sep 26, 2021
Automated Evidence Collection for Fake News Detection

Automated Evidence Collection for Fake News Detection This is the code repo for the Automated Evidence Collection for Fake News Detection paper accept

Mrinal Rawat 2 Apr 12, 2022
Geneva is an artificial intelligence tool that defeats censorship by exploiting bugs in censors

Geneva is an artificial intelligence tool that defeats censorship by exploiting bugs in censors

Kevin Bock 1.5k Jan 06, 2023
Code for paper "Vocabulary Learning via Optimal Transport for Neural Machine Translation"

**Codebase and data are uploaded in progress. ** VOLT(-py) is a vocabulary learning codebase that allows researchers and developers to automaticaly ge

416 Jan 09, 2023
ALBERT: A Lite BERT for Self-supervised Learning of Language Representations

ALBERT ***************New March 28, 2020 *************** Add a colab tutorial to run fine-tuning for GLUE datasets. ***************New January 7, 2020

Google Research 3k Jan 01, 2023