Reloading submodules in IPython
IPython comes with some automatic reloading magic:
%load_ext autoreload
%autoreload 2
It will reload all changed modules every time before executing a new line. The way this works is slightly different than dreload
. Some caveats apply, type %autoreload?
to see what can go wrong.
If you want to always enable this settings, modify your IPython configuration file ~/.ipython/profile_default/ipython_config.py
[1] and appending:
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
Credit to @Kos via a comment below.
[1]
If you don't have the file ~/.ipython/profile_default/ipython_config.py
, you need to call ipython profile create
first. Or the file may be located at $IPYTHONDIR
.
IPython notebook: how to reload all modules in a specific Python file?
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
Autoreload of modules in IPython
For IPython version 3.1, 4.x, and 5.x
%load_ext autoreload
%autoreload 2
Then your module will be auto-reloaded by default. This is the doc:
File: ...my/python/path/lib/python2.7/site-packages/IPython/extensions/autoreload.py
Docstring:
``autoreload`` is an IPython extension that reloads modules
automatically before executing the line of code typed.
This makes for example the following workflow possible:
.. sourcecode:: ipython
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
The module was reloaded without reloading it explicitly, and the
object imported with ``from foo import ...`` was also updated.
There is a trick: when you forget all of the above when using ipython
, just try:
import autoreload
?autoreload
# Then you get all the above
reload Python module within Jupyter notebook (without autoreload)
You need to import src
too and then reload(src.mymodule)
.
from src import mymodule
import src
# Change in mymodule
reload(src.mymodule)
IPython reloading different module?
reload
is supposed to repeat the process of locating the source code for the module it's reloading, and if it finds a different file from what the original import found, it's supposed to use the new file. After all, it needs to handle cases where a module was moved, or where a normal module was changed to a package or an extension module.
reload
is not supposed to look at sys.modules
and stop if it finds something. If it did that, it would perform no reloading!
The reason reload
finds the local string.py
file when the initial IPython-internal import didn't is because the import path has changed since the first import. You ran IPython in a way that doesn't cause Python itself to put the working directory on the module search path, and IPython imported the string
module from the standard library under that configuration. Afterward, IPython placed the working directory on the module search path itself, mimicking regular interactive Python, so reload
found the local string.py
.
How to refresh a Python import in a Jupyter Notebook cell?
I faced a similar issue , while importing a custom script in jupyter notebook
Try importing the module as an alias
then reloading it
import Nash as nash
from importlib import reload
reload(nash)
How can I reload objects in my namespace in ipython
Python 2:
reload(module)
Python 3:
from importlib import reload
reload(module)
Where module
is the file with your functions.
Related Topics
Saving a Numpy Array as an Image
How to Compare Version Numbers in Python
How to Access Command Line Arguments
How to Get a Directory Listing Sorted by Creation Date in Python
Pandas: Adding New Column to Dataframe Which Is a Copy of the Index Column
Efficiently Using Multiple Numpy Slices for Random Image Cropping
Download File from Web in Python 3
Elegant Ways to Support Equivalence ("Equality") in Python Classes
Importerror: No Module Named Requests
What Is a Cross-Platform Way to Get the Home Directory
Apply VS Transform on a Group Object
Django Media_Url and Media_Root
Split a String by Spaces -- Preserving Quoted Substrings -- in Python
Shooting a Bullet in Pygame in the Direction of Mouse
How to Detach Matplotlib Plots So That the Computation Can Continue