39

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:

  1. modify myFile
  2. restart the Ipython kernel
  3. 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!

0

3 Answers 3

76

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

Sign up to request clarification or add additional context in comments.

4 Comments

I do not like the last remark ("if you rename.."): Why can't Jupyter do that automatically?
The magic number 2 - what is this referring to? %autoreload 2
On my side, the created file was ~/.ipython/profile_default/ipython_config.py
17

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

2 Comments

This will not work if you imported your objects like from my_awesome_python_script import MyAwesomeClass. You will get my_awesome_python_script is not defined
This does not work in my distribution (python 3.10 jupyter 1.0.0).
-12

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"

6 Comments

But this would also require to restart the IPython kernel, right?
@WeiChen No, you don't have to restart the iPython kernel, you can do "Cell->Run all" anytime.
But that does not work, since the Ipython notebook will not import the latest version of myFile by running "Run all", instead it will assume that it has already been imported and skip that cell.
Do not know why this was down-voted but it is a rational answer, works for me,
@JakeWagner Yeah, sometimes I am not sure why people down-vote answers so quickly. In any case, thanks for the up-vote!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.