I am using Python + IPython for Data Science. I made a folder that contains all the modules I wrote, organised in packages, something like
python_workfolder
|
|---a
| |---__init__.py
| |---a1.py
| |---a2.py
|
|---b
| |---__init__.py
| |---b1.py
| |---b2.py
|
|---c
| |---__init__.py
| |---c1.py
| |---c2.py
|
|
|---script1.py
|---script2.py
At the beginning of each session I ask IPython to autoreload modules:
%load_ext autoreload
%autoreload 2
Now... let's say a1.py contains a class, A1
, that I want to call from one of the scripts. In the __init__.p
of package a
I import the module
import a1
Then in the script I import the class I need
from a.a1 import A1
If there is some error in class A1 and I modify it, there is no way to have Python reload it without restarting the kernel.
I tried with del a1
, del sys.modules['a1']
, del sys.modules['a']
. Each time it uses the old version of the class until I don't restart the kernel... anyone can give me some suggestions?
Old thread, but I had the same problem, so here is the solution I found. You have to use the module
sys
and before importinga1
write the followingsys.modules.pop('a1')
:The module is then reloaded.