How to Refresh an Imported Python File in a Jupyter Notebook

If you have imported a python file and later make changes to it, you’ll need to reload it in your Jupyter Notebook to take advantage of any recent changes.

Here’s the scenario. You are working in a Jupyter Notebook and you’ve imported a custom python file. While working in your notebook, you make some changes to the python file and want to work with those new changes in your Jupyter Notebook. After saving your python file, you run your import some_file code again. However, your recent changes don’t get imported.

You could restart your entire kernel, or you can simply reload the file by running this code:

import importlib
importlib.reload(python_file)

Once you run the code, you’ll see that any changes you made to your python file are correctly loaded into your jupyter notebook.


Posted

in

by

Tags: