Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid restarting Jupyter kernel in package develop mode?

I am working on a python package as a developer. The package is stored in a git repo and I use a local branch to debug/fix this package.

I use Jupyter notebooks using pip in edit mode to load my local branch as a package, where I test changes I make.

I run the following steps:

  1. Load the local package in a develop mode
  2. Import the module I want to test
  3. Do the test

For instance:

! pip install -e Path/To/Local/Package/ # step 1

import local_foo as foo # step 2
foo.print() # step 3

After step 3 if the code doesn't behave as expected, I correct my package, restart the jupyter kernel, and re-run the 3 previous steps until I get the behavior I want.

My question is:
Is there a way to avoid restarting the kernel?

I tried the following but it doesn't work in this case:
IPython autoreload:

%load_ext autoreload
%autoreload 2

and importlib.reload:

import importlib
importlib.reload(foo)

I tried the solution suggested in this article:
https://support.enthought.com/hc/en-us/articles/204469240-Jupyter-IPython-After-editing-a-module-changes-are-not-effective-without-kernel-restart

Many thanks!


PS: In addition, can some of you can share tips, workflows or experiences using Jupyter notebooks to manage python package development (test,...)

like image 244
David Leon Avatar asked Dec 10 '25 00:12

David Leon


1 Answers

You can use autoreload, adding these two lines at the beginning of your Jupyter notebook:

%reload_ext autoreload
%autoreload 2
like image 109
PieCot Avatar answered Dec 11 '25 13:12

PieCot