IPython notebook: how to reload all modules in a specific Python file?
Question:
I define many modules in a file, and add from myFile import *
to the first line of my ipython notebook so that I can use it as dependency for other parts in this notebook.
Currently my workflow is:
- modify
myFile
- restart the Ipython kernel
- rerun all code in Ipython.
Does anyone know if there is a way to reload all modules in myFile
without need to restart the Ipython kernel? Thanks!
Answers:
You should you start your workflow after restarting and opening a notebook again by running all cells. In the top menu, before you do anything else, first select “Cell->Run all”
From the ipython docs:
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: from foo import some_function
In [4]: some_function()
Out[4]: 42
In [5]: # open foo.py in an editor and change some_function to return 43
In [6]: some_function()
Out[6]: 43
You can also configure the auto reload to happen automatically by doing this:
ipython profile create
and adding the following to ~/.config/ipython/profile_default/ipython_config.py
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
c.InteractiveShellApp.exec_lines.append('print("Warning: disable autoreload in ipython_config.py to improve performance.")')
Note: If you rename a function, you need to rerun your import
statement
Use importlib!
import importlib
importlib.reload(my_awesome_python_script)
So when you do changes in your my_awesome_python_script
in the backend, no need to restart the kernel or re-run the entire notebook again. Just re-run this cell.
This is extremely useful if you did a lot of work on memory heavy datasets
I define many modules in a file, and add from myFile import *
to the first line of my ipython notebook so that I can use it as dependency for other parts in this notebook.
Currently my workflow is:
- modify
myFile
- restart the Ipython kernel
- rerun all code in Ipython.
Does anyone know if there is a way to reload all modules in myFile
without need to restart the Ipython kernel? Thanks!
You should you start your workflow after restarting and opening a notebook again by running all cells. In the top menu, before you do anything else, first select “Cell->Run all”
From the ipython docs:
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: from foo import some_function
In [4]: some_function()
Out[4]: 42
In [5]: # open foo.py in an editor and change some_function to return 43
In [6]: some_function()
Out[6]: 43
You can also configure the auto reload to happen automatically by doing this:
ipython profile create
and adding the following to ~/.config/ipython/profile_default/ipython_config.py
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
c.InteractiveShellApp.exec_lines.append('print("Warning: disable autoreload in ipython_config.py to improve performance.")')
Note: If you rename a function, you need to rerun your import
statement
Use importlib!
import importlib
importlib.reload(my_awesome_python_script)
So when you do changes in your my_awesome_python_script
in the backend, no need to restart the kernel or re-run the entire notebook again. Just re-run this cell.
This is extremely useful if you did a lot of work on memory heavy datasets