Clearing a session and reload() problem (with repro error)

Gabriel Genellina gagsl-py2 at
Tue Sep 9 18:03:33 CEST 2008

En Mon, 08 Sep 2008 05:37:24 -0300, Rafe <rafesacks at> escribió:

> I've tried to use reload with a very simple algorithm. Simply run
> through every imported module, ignoring anything that is "None" or on
> the C: drive (all of our python is on a network drive so this hack
> works for me for now) and reload() it. I've come to realize that this
> isn't near intelligent enough to handle sub-packages.

> Package Structure:
> ---------------------------
> inheritRepro
>         child

To summarize your code: only contains the reload  
support; baselib is an independent module; child/ imports and  
uses baseLib.

> 	# Iterate over all IMPORTED modules
> 	modules = sys.modules
> 	for modName in modules:
> 		mod = modules[modName]

(note: sys.modules could change along the iteration; I'd iterate this way  

	modules = sys.modules
	for modName in modules.keys():
		mod = modules.get(modName)

Reloading Python Modules...
Reloaded <module 'inheritRepro' from  
Reloaded <module 'inheritRepro.child' from  
Reloaded <module 'inheritRepro.baseLib' from  

Note that child is reloaded *before* baseLib - when child imports baseLib,  
it still finds the old module. One should reload baseLib before reloading  
child, in that case it works fine.

This dependency between modules, applied to all modules in your project,  
defines a "dependency graph". In some cases, one can define a partial  
ordering of its nodes, such that no module depends on any other module  
*after* it (it may depend only on modules *before* it). Look for  
"topological sort".

Doing that in the generic case is not easy. If you *know* your  
dependencies, reload the modules in the right order by hand.

> NOTE: this works if I don't use a sub-package for 'child' (
> instead). Is it overly simple to assume reloading by file structure
> might work?

You're iterating over sys.modules in whatever ordering the keys are in the  
dictionary; using other module names may yield the "right" ordering just  
by chance.

Gabriel Genellina

More information about the Python-list mailing list