Last active
January 14, 2023 04:06
-
-
Save atvKumar/22c9f9f01d2694eebd73 to your computer and use it in GitHub Desktop.
Python Module Reloading and ReCompiling for Maya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import path | |
import compileall | |
import sys, types | |
def recompile(modulename): | |
"""Recompile the given module, its directory's contents!""" | |
myScriptPath = sys.modules[modulename.__name__].__path__[0] | |
if path.isdir(myScriptPath): | |
compileall.compile_dir(myScriptPath, force=True) | |
def reload_module(modulename): | |
"""Reload the given module and all children""" | |
# Get a reference to each loaded module | |
loaded_modules = dict([(key, value) for key, value in sys.modules.items() | |
if key.startswith(modulename.__name__) and | |
isinstance(value, types.ModuleType)]) | |
# Delete references to these loaded modules from sys.modules | |
for key in loaded_modules: | |
del sys.modules[key] | |
# Load each of the modules again | |
# Make old modules share state with new modules | |
for key in loaded_modules: | |
print 're-loading %s' % key | |
newmodule = __import__(key) | |
oldmodule = loaded_modules[key] | |
oldmodule.__dict__.clear() | |
oldmodule.__dict__.update(newmodule.__dict__) | |
def refresh(modulename): | |
recompile(modulename) | |
reload_module(modulename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are welcome, been a while since I got anyone thanking me. Almost forgot I had this gist up.