Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter autoreload workflow

I'm trying to understand the best workflow for impotring script files into a jupyter notebook.

I have a notebook that does somethig like:

%load_ext autoreload
%autoreload 2
import functions as F

Inside functions.py, I further do imports such as

import numpy as np
import mymodule

It seems then that, for example, numpy will get reloaded every time I execute a cell, which makes things a bit slow. How could I automatically reload functions.py without reloading the imports there that I never change?

like image 366
theQman Avatar asked Mar 12 '26 01:03

theQman


2 Answers

I don't quite understand your question. The main functionality of %autoreload is to automatically reload modules, what it does, according to you. You can read about it here, I find it pretty well explained.

However, if you need to access import internals, you should take a look at importlib and especially importlib.reload():

import importlib
importlib.reload(my_module)

or

from importlib import reload
reload(my_module)

It is available starting from Python 3.1.

like image 73
Kami Avatar answered Mar 13 '26 13:03

Kami


This can be achieved by specifying the configuration option(%autoreload 1) to auto-reload a selected module.

However, the module must be imported as %aimport my_module.

like image 44
Oyekemi Avatar answered Mar 13 '26 14:03

Oyekemi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!