Messing with sys.modulesĭavid Beazley also mentions in his talk that the instance actually recording imported modules is located in sys.modules which happens to be a standard python dict. If you find yourself playing with the idea of reimporting modules in software that’ll be used productively consider alternatives such as writing unittests (if you’re using it to test code while developing), using multi-/subprocess to run the code in a separate interpreter, refactoring or simply restarting the interpreter.Īlso in this article I deliberately try to make programs fail and break which is intended to explore features of the interpreter and standard library and not meant to be done in productive software. It can cause severe bugs which can be virtually impossible to track down. As you’ll see towards the end, things can get very messy very quickly when you reimport modules. I’d like to take this opportunity to disclaim here immediately and sort of spoil the conclusion by stating that you should absolutely avoid reimporting any modules in python. The canonical albeit still bad way is to import importlib and use importlib.reload(module). Reimporting in Python - BasicsĪs David mentions, in python you cannot just import module again to reload the module. Though I appreciate that is not very pretty.So I am sitting here watching David Beazley’s pycon talk about modules, packages and imports and he is talking about sys.modules sort of guarding multiple imports which inspired me to fire up the python interpreter myself and start messing about. I think the problem with your code is that due to scope, the loaded variable is reset when you run the Toolkit restart, so it is never True? The environment variables should survive the reload. _command("Show Starter Template App.", self.show_app) If "MESSAGE_HELPER_PRELOADED" in os.environ: # Append path to message helper module location I just mocked up an example on my side, and whilst Toolkit won’t reload any modules that it didn’t import (I’m not actually a 100% on what the requirements are there), you should be able to reload them with a reload statement, but you would need to handle that your self. # when reloaded, loaded is defined as true, so reload your submodules:ĭo you know any way to force python / sgtk to reload all submodules without exiting maya? Right now, everytime I make a synta圎rror, it takes me several minutes juste to relaunch maya… # loaded is then defined in the exception block # First time the module imports, loaded is undefined and will trigger the exception. Module B is outside config folder, in a cross project directoryĪll init.py are setuped to reload submodules: I am using a decentralised setup (descriptor pointing to a folder on our sever) Even ‘Reload engine and apps’ in Maya Shotgun’s Work Area Info does not seem to do the trick. My problem is that SGTK does not seem to reload module B if I don’t completly close and restart maya. I’m currently working on a publish pipeline, using STK to do checks and publish operations with my own modules.Įnv hook tk-multi-publish2 loads a module A, which loads a module B, which does some stuff.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |