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.
I finally found the answer:
after editing
MyPak/MyMod.pyfile, to reload the classMyModin the fileMyMod.py, one needs toCaveats:
Executing
del MyPakordel MyModordel MyPak.MyModdoes not solve the problem since it simply removes the name binding. Python only searchessys.modulesto see whether the modules had already been imported. Check out the discussion in the post module name in sys.modules and globals().When reloading MyPak, python tries to execute the line
from MyMod import MyModinMyPak/__init__.py. However, it findsMyPak.MyModinsys.modules, thus it will NOTreload
MyModalthoughMyPak/MyMod.pyhas been updated. And you will find that no newMyPak/MyMod.pycis generated.