Clearing a session and reload() problem (with repro error)
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Sep 9 12:03:33 EDT 2008
En Mon, 08 Sep 2008 05:37:24 -0300, Rafe <rafesacks at gmail.com> 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
> __init__.py
> baseLib.py
> child
> __init__.py
To summarize your code: inheritRepro.__init__.py only contains the reload
support; baselib is an independent module; child/__init__.py 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
instead)
modules = sys.modules
for modName in modules.keys():
mod = modules.get(modName)
Output:
Reloading Python Modules...
Reloaded <module 'inheritRepro' from
'C:\TEMP\problema_con_reload\inheritRepro\_
_init__.pyc'>
Reloaded <module 'inheritRepro.child' from
'C:\TEMP\problema_con_reload\inheritR
epro\child\__init__.pyc'>
Reloaded <module 'inheritRepro.baseLib' from
'C:\TEMP\problema_con_reload\inheri
tRepro\baseLib.pyc'>
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' (child.py
> 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