Import or reload Jython dynamically

I need to encode something in Jython for CCPS (a program using jython as a scripting interface). However, Jython does not update the submodules if I change them in the editor, unless I restart the program (the startup time is prohibitive). Testing and tuning SO is relatively slow.

I have googled and found a message indicating that I need to import or reload the submodules. Thus, the main circuit:

def loader(module, part=None): if not module in sys.modules : if part == None: exec("import "+module) else: exec("from %s import %s" % (module, part)) else : exec("reload "+module) 

however, I have a problem with this, the module loads locally, that is, I can access the module in the loader() function, but not in my main code.

Two questions:

What is the correct way to test something with submodules in Jython without restarting Jython after changing each submodule? Is there a way to generate global variables dynamically so that I can import into a global namespace?

(e.g. exec("global %(mod)s = %(mod)s" % ({'mod':module}))

+4
source share
1 answer

How to simply unload all modules so that they are reloaded at the next import:

 import sys sys.modules.clear() 
+4
source

Source: https://habr.com/ru/post/1411741/


All Articles