Try reload except import - python dev in maya

I know this is dirty but is there a better way?


try:
    reload(myTools)
except:
    import myTools

basically forces a reload of a tools script imported into a GUI script…

chur

J

So if the goal is to ensure that folks are always loading the most recent version of tools you can generally do either of these.


import myTools
reload(myTools)

# OR

try:
    reload(myTools)
except NameError as e:
    import myTools

Now the second one, which is really just your example without the bare exception clause, can be a bit more performance friendly if you’re executing a lot of code when importing the module, whereas the first option will end up running that code twice.

I would caution against relying on reload to heavily, It works okay for single file scripts, but cross module, or inter package reloads can be iffy.

Cheers bro… yeah initially this is only to handle single imported modules, making sure any updates to that script are indeed loaded.
I’d say a more elegant solution would be needed where multiple modules are involved… something like tracking only updated files and reloading those…

thanks for the tip…

Jamie

while developing that is ok, but when its time to deploy tools to a team, its better to devise a system of packing up changes to distribute as a whole.

for example, we release builds to the team twice a week. tech artists, while working, will use import/reload, but when take that out for the end user. they don’t need to check for updates every time a script is run since we distribute our changes on a regular basis.

You probably want to think about the state of the entire environment as the main work unit rather than the state of any individual file. It’s very easy to be screwed – both by obvious things ( I move something from module A to module B, but the reloaded version of B gets confused because it still has old A in memory) and by subtler things (I have a pyc for a module left over after I convert to a package – so I never see changes to the package because Python never sees an updated file for that module again). You should update the whole environment in one go at the beginning of a session and don’t re-update until the next session.

We use zip files with the whole shebang as one thing, which are always grabbed from a central server that has the latest, tested, working build when the user starts up; “getting latest” is just “restarting maya”