"rehash" Equivalent For Python Modules

is it possible to reload my own Python files, much like there's an option for MEL files through rehash() function? i'm trying to move some source into module-based architecture, and whenever i run something in maya, it caches first version of an included file, and then does not react to changes done to .py files. it's just impossible to debug anything this way, if restarting maya is the only option to reload my .py files.

QUOTE(uiron @ 09/16/08, 05:01 PM) [snapback]291750[/snapback]
is it possible to reload my own Python files, much like there's an option for MEL files through rehash() function?

Rehash does not re-read the files, so i wonder if that would work out for you at all (but source does!). It just updates the lookup accelerator. A loaded function in mel would still be loaded. In sense it works just like pythons import, you would need to re source it again in maya. Python does not need to rehash because its not using the load accelerator like maya does. It will know if a new file is inserted even if it wasnt put to library at start of maya, much akin to how maya used to work piror to rehash. (this said rehash isnt needed or even help in loading the file anew unless its there with a new name or place on them. This applies only for mel python manages them directly form disk)

In python a already imported module is loaded. You can ensure python releoads it use the python finction reload() Theres a catch to reloading tough. Read:

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

Problem is that you need to reload modules in right order if they import into one anouther.

What I have found is that there is not a simple way to reload Python scripts with Maya. I think the query about looking for a command that functions like "rehash" is very valid as without it development of scripts becomes very cumbersome as restarting Maya is the only way to apparently reload one's updated Python scripts into Maya.

In the process of working on a script when you want to see the changes make certain that you call it withPython in Maya using:

reload(myPythonScript)

This is an undocumented Maya Python command which ought to be in the documentation, but is not.

I think you may have meant to say earlier that you wanted something which did the same thing as source(myMelScript) ;. I had made the mistake of thinking I was looking for something like rehash; - which is not needed (as Joojaa correctly points out) for Python when adding new scripts to a defined Python path. The act of calling it checks to see if it is one of the defined paths.

Good luck and happy Python code.

QUOTE(meesto @ 01/22/09, 04:41 AM) [snapback]299368[/snapback]
reload(myPythonScript)

This is an undocumented Maya Python command which ought to be in the documentation, but is not.

Its not undocumented. Its just not a Maya Python command its a python command so its documentted win the python documentation. Autodesk only documents the maya api part you need to learn base python features juat like any other by reading pythons documentation (otherwise the burden of documenting maya would be too big, and the benefit form python would be 0)

With python you kindof need 2 hats at once. Just like in c++ api development as autdesk doesnt teach you how to write c++ either.

QUOTE(meesto @ 01/22/09, 04:41 AM) [snapback]299368[/snapback]
In the process of working on a script when you want to see the changes make certain that you call it withPython in Maya using:

reload(myPythonScript)

Good luck and happy Python code.

thank you ... i`m starting to write some tools in python and this piece of informations is invaluable

Yes but read the link i provided theres a caveat in reload! And that is if you intend to use scripts that call scripts you need to RELOAD them in correct order.

Somedbody had posted a way to dynamically reload all modules recursively in python somewhere search for that. It will come handy to you in the long run. Saves a LOT of headaches that reload simply doesn't do.

QUOTE(Joojaa @ 03/04/09, 10:58 PM) [snapback]302277[/snapback]
Somedbody had posted a way to dynamically reload all modules recursively in python somewhere search for that. It will come handy to you in the long run. Saves a LOT of headaches that reload simply doesn't do.

that`s good to know ... thank you

Just wasted 2 weeks because somebody dynamical reloaded stuff in system on fly in wrong order