for my Master Thesis, I'm trying to leverage Python's nature of being a scripting language in order to allow runtime changes of the source code. The source code itself is modelled using a special user interface and generated on save. On start, the generated code is sent to the engine and imported. This process works fine until the source code changes without restarting the engine between two runs (but restarting is no option).
The problem is that the engine still holds a reference to the old source code even though the file has been changed.
I spent quite some time searching for solutions and found reload() as one option (ref). Unfortunately, this doesn't work for some reason. Here is the current version of the relevant part (executed each time source code changes are received):
package = __import__("tmp", fromlist=["tmp"])
reload(package) # this seems to have no effect
clsmembers = inspect.getmembers(package, lambda member: inspect.isclass(member) and member.__module__ == package.__name__)
beclass = clsmembers[0][1]
be = beclass()
I'm working with Python 2.7 (I have to, I know later Python versions offer better support for this feature). It doesn't have to be reload(), I just need any way to re-import the code changes.
Any help is appreciated! :)