86

If I import a module defining a class of the same name belonging to a package, it is imported as a Class, not a Module because of the __init__.py of the parent package. See different import results at different directories for details. In Python shell or ipython shell, if I do

from MyPak import MyMod

MyModule is always imported as Class thus I can not reload it (reload() works only for modules). Run

from MyPak import MyMod

again does not seem to update the Class definition. Could anyone suggest a way to update the class in python shell?

ps. without restarting the python interpreter.

pps. Just in case you have the code in hand and want to test it: I am actually talking about BioPython, and I am working on Bio.PDB.PDBParser. I have an ipython shell (v0.10) and edit PDBParser.py. Just got no way to reload it in ipython.

so here is what I did:

# start ipython v0.10
import Bio
from Bio.PDB import PDBParser
p = PDBParser()
s = p.get_structure()
# then I make changes,e.g. simply print some text, in PDBParser.py
del Bio
del PDBParser
del s
import Bio  # or reload(Bio) without deleting all the objects
from Bio.PDB import PDBParser
p = PDBParser()
s = p.get_structure() # expected output after change not seen :(

I could not see the printed text. The changes were not applied somehow.

5
  • 1
    CTRL+C, $ python, from MyPak import MyMod ;)
    – Jacob
    Commented Aug 4, 2011 at 18:09
  • Would import MyPak.MyMod then reload(MyPak.MyMod) work?
    – agf
    Commented Aug 4, 2011 at 18:12
  • no, import MyPak.MyMod still imports MyPak.MyMod as a class.
    – HongboZhu
    Commented Aug 4, 2011 at 18:14
  • 1
    What about del MyMod, then from MyPak import MyMod? Your package layout is a little unusual (with with MyPak/__init__.py importing everything) but that may work to clear the namespace and allow you to re-import it. Commented Aug 4, 2011 at 18:54
  • @Kirk Strauser: still no luck. As a matter of fact, I have done that before. I am deeply confused.
    – HongboZhu
    Commented Aug 5, 2011 at 8:23

7 Answers 7

108

On Python 3 only, import the reload function:

>>> from importlib import reload

On both Python 2.x, and 3.x, you can then simply call reload on the module:

>>> import MyPak
>>> reload(MyPak)
>>> from MyPak import MyMod

However, instances of the old class will not be updated (there's simply no code that describes the update mechanism).

8
  • Just tested it. It seems this does not update MyMod, either. I actually have MyPak1.MyPak2.MyMod. But I don't think that should be the reason :(
    – HongboZhu
    Commented Aug 4, 2011 at 18:19
  • 5
    isn't working for me in Python 3.3. What version are you using? Commented Aug 1, 2014 at 18:45
  • 2
    @AllTradesJack On Python 3, you need to import reload first: from importlib import reload.
    – ostrokach
    Commented Dec 26, 2016 at 17:18
  • 3
    This does not work for classes when I tried it in python3.6.
    – Josh
    Commented Mar 24, 2019 at 12:55
  • 2
    @Josh you can only reload modules, not classes themselves.
    – powlo
    Commented Aug 12, 2019 at 12:56
48

I finally found the answer:

import MyPak
from MyPak import MyMod

after editing MyPak/MyMod.py file, to reload the class MyMod in the file MyMod.py, one needs to

import sys
del sys.modules['MyPak.MyMod'] 
reload(MyPak)
from MyPak import MyMod

Caveats:

  1. Executing del MyPak or del MyMod or del MyPak.MyMod does not solve the problem since it simply removes the name binding. Python only searches sys.modules to see whether the modules had already been imported. Check out the discussion in the post module name in sys.modules and globals().

  2. When reloading MyPak, python tries to execute the line from MyMod import MyMod in MyPak/__init__.py. However, it finds MyPak.MyMod in sys.modules, thus it will NOT reload MyMod although MyPak/MyMod.py has been updated. And you will find that no new MyPak/MyMod.pyc is generated.

4
  • 2
    reload is not recognized as a command for me in 3.3. What version are you using? Commented Aug 1, 2014 at 18:48
  • Given caveat number 2, perhaps this would work: import MyPak.MyMod; reload(MyPak.MyMod); import MyPak; reload(MyPak); from MyPak import MyMod. Except that this approach wouldn't scale to more complex cases (in terms of readability). The suggested solution seems cleaner, as it seems to guarantee that the module is erased from the environment. Commented Oct 28, 2015 at 11:57
  • On 3.3 do from imp import reload to get reload() into the system
    – Yona
    Commented Feb 19, 2016 at 9:36
  • On 3.5 I did import importlib before importlib.reload().
    – Daniel
    Commented Oct 21, 2016 at 17:41
25

There are thee ways to solve this:

1. Use import MyPak.MyMod instead of from MyPak import MyMod

Then you can write:

from importlib import reload  # If on Python 3
import MyPak.MyMod
reload(MyPak.MyMod)

and it works.

2. Use IPython.lib.deepreload

from MyPak import MyMod
from IPython.lib.deepreload import reload
reload(MyPak)  # This should also reload all submodules

3. Use autoreload magic

%load_ext autoreload
%autoreload 2
import MyPak.MyMod  # All changes to MyPak.MyMod will be loaded automatically
1
  • 1
    I almost coded up my own deep reload recently...glad I did not waste my time. This is a super useful utility. Commented Dec 20, 2019 at 15:43
13

You can use a magic function:

%load_ext autoreload
%autoreload 2
from MyPak import MyMod

It also works for function imports:

%load_ext autoreload
%autoreload 2
from anypythonfile import my_function

This magic function works in Python 2.x (I've tested on 2.7+) and Python 3.x (I've tested on 3.7).

6

I have one myfile.py file which contains one class MyClass

To import just do:

from myfile import MyClass
mc = MyClass()

To reload:

import sys
del sys.modules['myfile']
from myfile import MyClass
modifiedmc = MyClass()

This is very useful while building modules. one can put these inside a function and just call the function

def myreload():
   import sys
   del sys.modules['myfile']
   from myfile import MyClass
   modifiedmc = MyClass()
   global mc
   mc = modifiedmc
0

It works for me using python 3.5.2:

import importlib
importlib.reload(class)
from class import module
1
  • The only one who shows how to import the "reload" function.
    – VoidMain
    Commented Apr 20, 2021 at 20:23
0

Here is shortest answer if you use Jupyter Notebook. You need change cell with

from your_function_name import YourClassName

to

from importlib import reload
import YourClassName
reload(YourClassName)
from YourClassName import your_function_name 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.