In a ESP32/MicroPython based project I want to (re)load modules from RAM without having to write them to the flash-based filesystem first. (this is both time consuming and wearing off the flash memory)
So my idea was to retrieve e.g. module.py
via web and then turn it into an actual module by using __import__
, exec()
and so on. But I don't know how.
Actually what I need is quite similar to this: How to load a module from code in a string?
In MicroPython there are no imp
or importlib
or even types.ModuleType
module but at least you have __import__
.
Is there a way to implement
my_code = 'a = 5'
mymodule = imp.new_module('mymodule')
exec(my_code, mymodule.__dict__)
Without imp.new_module
?
I tried sys.__class__('mymodule')
in order smuggle the module type out of an existing module but I get
>>> mymodule = sys.__class__('mymodule')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't create 'module' instances