Reload modules#
By default, IPython will only read imported modules when their import
is mentioned for the first time. But this option allows you to set some specific rules for importing modules. Official documentation.
In essence this extension doles out two magic %autoreload
and %aimport
:
%autoreload
- specifies the import mode;%aimport
- sets exceptions for%autoreload
if they are provided by the current mode.
Note look like you should use the full path to the files if you want to mention them for %aimport
.
%autoreload 1/explicit
#
Before executing a cell, only modules mentioned with %aimport
will be reloaded.
In the following example I:
Create two files
fun<i>
\(i \in {1,2}\), each containing only one function that returns a line like"Original line from fun<i>.py"
;Import them using the
%autoreload explicit
magic and mentioningfun1
in%aimport
.Call them, and got obvious result -
Original line from fun<i>.py
;Then change both functions to return
New line from fun<i>.py
;Call the functions again:
Function from
fun1
has changed the line;Function from
fun2
still has the wordOriginal
at the beginning (because it was mentioned in%aimport
as module to me reloaded).
%%bash
cat > reloading_modules_files/fun1.py << EOF
def return_line():
return "Original line from fun1.py"
EOF
cat > reloading_modules_files/fun2.py << EOF
def return_line():
return "Original line from fun2.py"
EOF
%load_ext autoreload
%autoreload explicit
from reloading_modules_files import fun1, fun2
%aimport reloading_modules_files.fun1
print(fun1.return_line())
print(fun2.return_line())
Original line from fun1.py
Original line from fun2.py
%%bash
cat > reloading_modules_files/fun1.py << EOF
def return_line():
return "New line from fun1.py"
EOF
cat > reloading_modules_files/fun2.py << EOF
def return_line():
return "New line from fun2.py"
EOF
print(fun1.return_line())
print(fun2.return_line())
New line from fun1.py
Original line from fun2.py
%autoreload 2/all
#
It’s the most common mode for me - reload all modules before running any cell except those mentioned in %aimport
.
If you want to exclude some modules from reimport, you should use %aimport -<module name>
(the “-” symbol is crucial here).
In the following example I:
Create two files
fun<i>
\(i \in {1,2}\), each containing only one function that returns a line like"Original line from fun<i>.py"
;Import them using the
%autoreload all
magic and mentioningfun2
in%aimport
.Call them, and got obvious result -
Original line from fun<i>.py
;Then change both functions to return
New line from fun<i>.py
;Call the functions again:
Function from
fun1
has changed the line;Function from
fun2
still has the wordOriginal
at the beginning (because it was mentioned in%aimport
as an exclusion).
%%bash
cat > reloading_modules_files/fun1.py << EOF
def return_line():
return "Original line from fun1.py"
EOF
cat > reloading_modules_files/fun2.py << EOF
def return_line():
return "Original line from fun2.py"
EOF
%load_ext autoreload
%autoreload all
from reloading_modules_files import fun1, fun2
%aimport -reloading_modules_files.fun2
print(fun1.return_line())
print(fun2.return_line())
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
Original line from fun1.py
Original line from fun2.py
%%bash
cat > reloading_modules_files/fun1.py << EOF
def return_line():
return "New line from fun1.py"
EOF
cat > reloading_modules_files/fun2.py << EOF
def return_line():
return "New line from fun2.py"
EOF
print(fun1.return_line())
print(fun2.return_line())
New line from fun1.py
Original line from fun2.py