470,594 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

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


This seems to be an old question, and I've read back a bit, but rather
than assume the answer is "you can't do that", I'd thought I'd post my
version of the question along with a reproducible error to illustrate
my confusion.

My problem is that I'm using Python inside XSI (a 3D graphics
application). If I want to restart Python, I have to restart XSI. This
is no small amount of time wasted.

The solution would be to somehow unload modules and all references and
allow my imports to rebuild the cache. I suppose it would be great to
clear the python session completely, but the 3D application might have
added something to the session at startup (I doubt this though as I
don't see any imported modules outside the norm).

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.

Before I post the repro, my questions are:
1) Does anyone have a work-flow for my situation that doesn't use
Reload, and doesn't involve restarting the app for every edit/fix

2) Can anyone help point me in the right direction to build a
dependable algorithm to cleanly reload all loaded modules? NOTE: I
don't need to maintain anything in memory (i.e. instances, pointers,
etc.) Everything will be initialized again each time. I'm not asking
for code. Just some ideas or pseudo-code would do nicely.

Here is a "simple" repro...

Package Structure:
import sys, os

def reloadModules():
Reload all imported modules that are not on the C: drive.
print "Reloading Python Modules..."

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

# Skip None types and other itesm we don't care about
if modName == "__main__" or not hasattr(mod,"__file__"):

# Get the drive and skip anything on the C: drive
drive = os.path.splitdrive(mod.__file__)[0]
if drive != "C:":
print "Reloaded %s" % mod
class BaseClassA(object):

class BaseClassB(BaseClassA):
def __init__(self):
super(BaseClassB, self).__init__()
import inheritRepro.baseLib as baseLib

class ChildClass(baseLib.BaseClassB):
def __init__(self):
super(ChildClass, self).__init__()
>>import inheritRepro
import inheritRepro.child as child
obj = child.ChildClass()
print obj
<inheritRepro.child.ChildClass object at 0x00B34C70>
>>inheritRepro.reloadModules() # Output omitted, but this worked.
import inheritRepro
import inheritRepro.child as child
obj = child.ChildClass()
Traceback (most recent call last):
File "<console>", line 0, in <module>
File "\\nas6tb\PROJECTS\tech\users\rafe.sacks\python\in heritRepro
\child\__init__.py", line 5, in __init__
super(ChildClass, self).__init__()
File "\\nas6tb\PROJECTS\tech\users\rafe.sacks\python\in heritRepro
\baseLib.py", line 6, in __init__
super(BaseClassB, self).__init__()
TypeError: super(type, obj): obj must be an instance or subtype of
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?

Right now I'm getting around this by reload()-ing the right package
after running reloadModules() if an error occurs. It's a bit
frustrating that it cost me two days of work before I realized it was
reload() causing this error and not super() or some other unknown-to-
me inheritance/package structure problem. I rebuilt my code module by
module until I noticed, quite by accident, that the thing worked once
and then never again! ....oh well, these are the joys of learning the
hard way.

I know this was a long one. If you made it this far, thanks for

- Rafe
Sep 8 '08 #1
0 1725

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by M Wells | last post: by
3 posts views Thread by Brad | last post: by
1 post views Thread by Werner | last post: by
8 posts views Thread by Tim W. | last post: by
9 posts views Thread by cashdeskmac | last post: by
11 posts views Thread by Bocah Sableng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.