Problem loading and reloading modules

113 views
Skip to first unread message

Lidia Martinez

unread,
Feb 20, 2015, 9:35:08 AM2/20/15
to python_in...@googlegroups.com

While developing i need to be compiling code all the time.

I've reached a point where i realised my files where not reloading, no matter what i told them.

I have my code inside a button, after watching Sublime Text failing when executing reload code inside:
if __name__== "__main__" :
...

I moved all the code to my shelf button, i tried importing and reloading at the same time, just to make it all reload and execute the new file. But it doesn't print what i tell him. This is not reloading...

The module contains some imports on the top and a class.

// button code

import moduleName

reload (moduleName)

from moduleName import className # doing * didn't work!

reload(moduleName) # ??... just trying crazy things


....


Yhy? Something i'm missing about python or compiler cache stuff in Maya?




--
Lidia

Marcus Ottosson

unread,
Feb 20, 2015, 10:04:49 AM2/20/15
to python_in...@googlegroups.com

Make sure you don’t have a .pyc version of your script lying around. And while you’re at it, disable the generation of those buggers permanently by setting PYTHONDONTWRITEBYTECODE to 1 in your environment. It’ll make sure you keep the hair on your scalp. :)

damon shelton

unread,
Feb 20, 2015, 10:08:42 AM2/20/15
to python_in...@googlegroups.com

You need to reload(className)
Even though you reloaded moduleName
ClassName is not getting reloaded in globals.

Reload won't work with import * unless you write code to remove all the objects from globals that import * creates first

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAB1%3D8wWQztaW0PwvXfAvrkEg7fyCdHq81-tuiwjH7_eZW4H%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Lidia Martinez

unread,
Feb 20, 2015, 10:54:01 AM2/20/15
to python_in...@googlegroups.com
Thanks Damon and Marcus!
Well, i tried doing reload of a the class inside the file, and it didn't work...

I'll see if it works once again.

The PYTHONDONTWRITEBYTECODE  stuff , i'll see how that works. 

Thanks!

--
Lidia

Marcus Ottosson

unread,
Feb 20, 2015, 10:58:25 AM2/20/15
to python_in...@googlegroups.com

The PYTHONDONTWRITEBYTECODE will only have an effect for newly compiled files. If this is the problem, you’ll need to manually remove the existing pyc file first.​

Marcus Ottosson

unread,
Feb 20, 2015, 11:34:19 AM2/20/15
to python_in...@googlegroups.com

If you print your module, you’ll see where it exists on disk, the .pyc file might lie right next to it. It’s also possible that you’ve got two modules with the same name in your PYTHONPATH, which would explain why you can import it, but not see the changes you make to the other one.​

Justin Israel

unread,
Feb 20, 2015, 1:53:16 PM2/20/15
to python_in...@googlegroups.com
@damon: reload only works on modules, right?

Usually when I run reload(), it takes care of loading from the py and not a pyc (you can usually confirm this by running reload manually and seeing the path it prints out). 

If your top level module imports other modules, whose source you are changing, you would need to "deep reload" all of those. That means all of the dependencies that changed have to be reloaded first, then the modules depend on them have to be reloaded. We have had some topic on reloading in the past, I think, with some extended info on this.

Code within 'if __name__ == "__main__"' shouldn't get executed under Maya. Is the particular project, for which you are having trouble, a single module or a multi-module project? The MayaSublime plugin should allow you to send the entire file for execution to Maya, in its own namespace. Have you tried that out? It doesn't use the import mechanism, but rather does an execfile on the source. 

On Sat Feb 21 2015 at 5:34:18 AM Marcus Ottosson <konstr...@gmail.com> wrote:

If you print your module, you’ll see where it exists on disk, the .pyc file might lie right next to it. It’s also possible that you’ve got two modules with the same name in your PYTHONPATH, which would explain why you can import it, but not see the changes you make to the other one.​

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Lidia Martinez

unread,
Feb 23, 2015, 4:29:44 AM2/23/15
to python_in...@googlegroups.com
Thanks!

The solutions on changing : sys.dont_write_bytecode = True   won't work for me. The reason is that i'm deploying this code for production and at the same time i'm working on development. If i set that variable to true, then the people who use the tool will be reloading the code all the time, that is slower.
If this was like C++ , where you can tell it to see if you are in a debug or release version... i guess that would be something to be added, a global variable in maya modified on the click of the shelf button, and have the button to test for developers and the released one... sounds quite shabby ( i guess that's the word, i don't find a proper sinonym for spanish word "cutre" :) )

About reloading first lower level modules that's a great idea. I'll test it.

This is a multi module project. Some common parts are laying inside separate files. I find it hard to make the reloading process useful while developing and at the same time be able to deploy for the departments to use it without any problem.

And about the mayaSublime plugin, that's what i'm using, but i also test the button itself, i inserted some import code on the execution.  That execfile and namespace stuff you told me, Justin, sounds interesting. Anywhere to search for more info?






--
Lidia

Justin Israel

unread,
Feb 23, 2015, 5:25:11 AM2/23/15
to python_in...@googlegroups.com

The execfile is just a standard python builtin :
https://docs.python.org/2/library/functions.html#execfile

The plugin uses it to create a namespace specifically for the plugin, and execs the file under it to avoid affecting the main  namespace of Maya, and to let it be persistent across calls.
https://github.com/justinfx/MayaSublime/blob/master/MayaSublime.py


Cesar Saez

unread,
Feb 23, 2015, 5:33:11 AM2/23/15
to python_in...@googlegroups.com

Lidia Martinez

unread,
Feb 23, 2015, 5:54:27 AM2/23/15
to python_in...@googlegroups.com
Cesar, that looks great. 

This catched my attention.
"If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes."

 Does it mean the class is not reloaded?, as someone said in the list?. Reload has some caveats i'm curious about...


--
Lidia

2015-02-23 11:32 GMT+01:00 Cesar Saez <ces...@gmail.com>:

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Marcus Ottosson

unread,
Feb 23, 2015, 6:16:11 AM2/23/15
to python_in...@googlegroups.com
If this was like C++ , where you can tell it to see if you are in a debug or release version... i guess that would be something to be added, a global variable in maya modified on the click of the shelf button, and have the button to test for developers and the released one...

I'm not sure this is the solution to your original problem, but if you'd like there is an environment variable for this called PYTHONDONTWRITEBYTECODE.

For more options, visit https://groups.google.com/d/optout.


--
Marcus Ottosson
konstr...@gmail.com


Justin Israel

unread,
Feb 23, 2015, 1:34:00 PM2/23/15
to python_in...@googlegroups.com


On Mon, 23 Feb 2015 11:54 PM Lidia Martinez <darksi...@gmail.com> wrote:

Cesar, that looks great. 

This catched my attention.

"If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes."

 Does it mean the class is not reloaded?, as someone said in the list?. Reload has some caveats i'm curious about...

Yea this is a caveat. Reloading won't help if other modules hold references to objects. All of the modules either have to be completely reloaded, or they have to access other modules though the module object.

--
Lidia

2015-02-23 11:32 GMT+01:00 Cesar Saez <ces...@gmail.com>:

This still works great for me:
https://groups.google.com/d/msg/python_inside_maya/sNqxa2yDt5s/CC9MNpFni50J

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi-d0zdmoqZZ9xPuTfcCZ%3DGxtmvBsn6XAO_KmOL3uVoteg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAB1%3D8z3yD6gWT0MtD2f%2Bb%2BOOY00y5_0EVpwVXr%3DDnqp7QXkGg%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages