reloading a script crashes maya

39 views
Skip to first unread message

King

unread,
Apr 12, 2009, 9:43:49 AM4/12/09
to python_inside_maya

The plugin I have created loads all the mel script and python scripts
at startup. All modules are imported using
"from myModule import *" syntax. In case if I do some changes in
python script and source again, maya crashes
badly. If I use "reload( myModule )" in script editor, it changes the
path to access functions. For example, now I have to use
"myModule.myFunction". The only solution is restart maya as far as
now.

Is there any workaround for this problem?

Prashant

Paul Molodowitch

unread,
Apr 12, 2009, 11:00:30 AM4/12/09
to python_in...@googlegroups.com
Unfortunately, you've run across one of the weakness of python -
dynamic reloading/resourcing.

There are some workarounds - for instance, with the problem you're
having, I'm assuming what's going on is you're doing something like
this:

>>> from myModule import myFunc

*** make some changes to myModule.myFunc

>>> reload(myModule)

...at which point, if you try to use myFunc, it hasn't been updated -
the problem is the reference in the global namespace still points to
the OLD version of the module, so you may be able to fix this by
re-doing

>>> from myModule import myFunc

Unfortunately, this quickly gets pretty unmanagable if there is any
sort of complexity to your dependency tree, since you'd have to go
through and reload all the dependencies in reverse order, etc.

I know I read once of somebody who implemented an
"automatic-rewinding" reloader, that worked by overloading the import
and keeping track of dependencies, but I never tried it myself, so I
have no idea how well it worked. (I was scared it would just be a
source of too many potential headaches / weird bugs). If you do try
it, lemme know.

But the short answer is... yeah, if you have any real complexity in
either your dependencies, or in your import namespacing, it's
generally easiest to restart.

- Paul

Ofer Koren

unread,
Apr 13, 2009, 12:11:54 AM4/13/09
to python_in...@googlegroups.com
Paul is right... avoid using
>>> from <module> import <object>
and you'll save yourself most of the trouble...

Another thing I do sometimes is to 'clear out' modules from python's internal modules dictionary, so that next time you run 'import' it'll act as if it was never loaded before (useful for reloading a python package):

def clearModules(moduleNames):
    """Clear any modules matching the string (or list of strings) provided from the python environment,
    so that they can be subsequently force-imported again."""
    
    import sys
    if not moduleNames:
        raise ValueError("Must supply a non-empty string/list")
        
    # convert to list
    if not hasatter(moduleNames, "__iter__"):
        moduleNames = [moduleNames]
    
    for m in sys.modules:
        for n in moduleNames:
            if (n in m) and sys.modules[m] is not None:
                print "Clearing module '%s', %s" % (m, sys.modules[m])
                del sys.modules[m]


Paul Molodowitch

unread,
Apr 13, 2009, 10:20:03 AM4/13/09
to python_in...@googlegroups.com
Hmm... that looks handy, thanks. I may try that next time I need to
re-import pymel. =)

Btw, here's a link to the 'rollback importer' i mentioned... on
another look, since it also just works by deleting stuff from
sys.modules, not sure it's worth it, since it would only work to get
back to fixed points (ie, wherever you installed it). Sounds like a
good solution if that's what you're looking to do, though...

http://pyunit.sourceforge.net/notes/reloading.html

- Paul
Reply all
Reply to author
Forward
0 new messages