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
git《Commonsense Knowledge Base Completion with Structural and Semantic Context》(AAAI 2020) GitHub: [fig1]

Commonsense Knowledge Base Completion with Structural and Semantic Context Code for the paper Commonsense Knowledge Base Completion with Structural an

AI2 96 Nov 05, 2022
A PyTorch implementation of EventProp [https://arxiv.org/abs/2009.08378], a method to train Spiking Neural Networks

Spiking Neural Network training with EventProp This is an unofficial PyTorch implemenation of EventProp, a method to compute exact gradients for Spiki

Pedro Savarese 35 Jul 29, 2022
Behavioral "black-box" testing for recommender systems

RecList RecList Free software: MIT license Documentation: https://reclist.readthedocs.io. Overview RecList is an open source library providing behavio

Jacopo Tagliabue 375 Dec 30, 2022
Explainable Zero-Shot Topic Extraction

Zero-Shot Topic Extraction with Common-Sense Knowledge Graph This repository contains the code for reproducing the results reported in the paper "Expl

D2K Lab 56 Dec 14, 2022
Predicting Tweet Sentiment Maching Learning and streamlit

Predicting-Tweet-Sentiment-Maching-Learning-and-streamlit (I prefere using Visual Studio Code ) Open the folder in VS Code Run the first cell in requi

1 Nov 20, 2021
python library for invisible image watermark (blind image watermark)

invisible-watermark invisible-watermark is a python library and command line tool for creating invisible watermark over image.(aka. blink image waterm

Shield Mountain 572 Jan 07, 2023
This repository provides the official implementation of 'Learning to ignore: rethinking attention in CNNs' accepted in BMVC 2021.

inverse_attention This repository provides the official implementation of 'Learning to ignore: rethinking attention in CNNs' accepted in BMVC 2021. Le

Firas Laakom 5 Jul 08, 2022
PyTorch Implementation of ECCV 2020 Spotlight TuiGAN: Learning Versatile Image-to-Image Translation with Two Unpaired Images

TuiGAN-PyTorch Official PyTorch Implementation of "TuiGAN: Learning Versatile Image-to-Image Translation with Two Unpaired Images" (ECCV 2020 Spotligh

181 Dec 09, 2022
SciFive: a text-text transformer model for biomedical literature

SciFive SciFive provided a Text-Text framework for biomedical language and natural language in NLP. Under the T5's framework and desrbibed in the pape

Long Phan 54 Dec 24, 2022
Block Sparse movement pruning

Movement Pruning: Adaptive Sparsity by Fine-Tuning Magnitude pruning is a widely used strategy for reducing model size in pure supervised learning; ho

Hugging Face 54 Dec 20, 2022
Time Series Forecasting with Temporal Fusion Transformer in Pytorch

Forecasting with the Temporal Fusion Transformer Multi-horizon forecasting often contains a complex mix of inputs – including static (i.e. time-invari

Nicolás Fornasari 6 Jan 24, 2022
A python toolbox for predictive uncertainty quantification, calibration, metrics, and visualization

Website, Tutorials, and Docs    Uncertainty Toolbox A python toolbox for predictive uncertainty quantification, calibration, metrics, and visualizatio

Uncertainty Toolbox 1.4k Dec 28, 2022
Transfer-Learn is an open-source and well-documented library for Transfer Learning.

Transfer-Learn is an open-source and well-documented library for Transfer Learning. It is based on pure PyTorch with high performance and friendly API. Our code is pythonic, and the design is consist

THUML @ Tsinghua University 2.2k Jan 03, 2023
SwinIR: Image Restoration Using Swin Transformer

SwinIR: Image Restoration Using Swin Transformer This repository is the official PyTorch implementation of SwinIR: Image Restoration Using Shifted Win

Jingyun Liang 2.4k Jan 05, 2023
Implementations of paper Controlling Directions Orthogonal to a Classifier

Classifier Orthogonalization Implementations of paper Controlling Directions Orthogonal to a Classifier , ICLR 2022, Yilun Xu, Hao He, Tianxiao Shen,

Yilun Xu 33 Dec 01, 2022
Frequency Spectrum Augmentation Consistency for Domain Adaptive Object Detection

Frequency Spectrum Augmentation Consistency for Domain Adaptive Object Detection Main requirements torch = 1.0 torchvision = 0.2.0 Python 3 Environm

15 Apr 04, 2022
MogFace: Towards a Deeper Appreciation on Face Detection

MogFace: Towards a Deeper Appreciation on Face Detection Introduction In this repo, we propose a promising face detector, termed as MogFace. Our MogFa

48 Dec 20, 2022
The sixth place winning solution (6/220) in 2021 Gaofen Challenge.

SwinTransformer + OBBDet The sixth place winning solution (6/220) in the track of Fine-grained Object Recognition in High-Resolution Optical Images, 2

ming71 46 Dec 02, 2022
Weakly Supervised Learning of Rigid 3D Scene Flow

Weakly Supervised Learning of Rigid 3D Scene Flow This repository provides code and data to train and evaluate a weakly supervised method for rigid 3D

Zan Gojcic 124 Dec 27, 2022
PyTorch implementation of ARM-Net: Adaptive Relation Modeling Network for Structured Data.

A ready-to-use framework of latest models for structured (tabular) data learning with PyTorch. Applications include recommendation, CRT prediction, healthcare analytics, and etc.

48 Nov 30, 2022