Loading and unloading a module dynamically at runtime

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Loading and unloading a module dynamically at runtime

Post by markxr » Mon Oct 03, 2016 12:53 pm

Hi,

I'm wondering if it's possible to workaround the small amount of RAM, by loading, then unloading several modules at runtime.

Then I can write more code than can fit in memory.

For example, if I have some pseudo-code which uses

Code: Select all

import sys

# ....

exec('import modtest', {} ) # Instead of globals, use empty dict, so we don't get a reference to the module
sys.modules['modtest'].some_func()
del sys.modules['modtest'] # Remove reference
This *appears* to work, i.e. it will free the memory used by "import modtest"

Does this seem like a reasonable thing to do?

Is it likely to continue to work in future versions of Micropython?

Mark

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Loading and unloading a module dynamically at runtime

Post by Damien » Tue Oct 04, 2016 9:43 am

Yes that seems reasonable and should continue to work in future versions.

Note that using frozen bytecode may help with memory problems (put your scripts in the esp8266/modules dir).

Sent from my GT-I9105P using Tapatalk

MasterOfGizmo
Posts: 50
Joined: Sun Nov 29, 2020 8:17 pm

Re: Loading and unloading a module dynamically at runtime

Post by MasterOfGizmo » Sun Nov 29, 2020 8:39 pm

markxr wrote:
Mon Oct 03, 2016 12:53 pm

Code: Select all

import sys

# ....

exec('import modtest', {} ) # Instead of globals, use empty dict, so we don't get a reference to the module
sys.modules['modtest'].some_func()
del sys.modules['modtest'] # Remove reference
This is working nicely for me with one small but important problem: I cannot do that twice. Deleting the module removes it from sys.modules as expected. But another import seems to run successful but does not reload it and it doesn't show up in sys.modules.

I am trying to use this mechanism to allow a user to select user defined scripts and to load and run them. Unfortunately I don't manage to allow the user to do this a second time. Is there a way to tell µP to load and run the same script a second time?

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Loading and unloading a module dynamically at runtime

Post by kevinkk525 » Mon Nov 30, 2020 8:10 am

Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

MasterOfGizmo
Posts: 50
Joined: Sun Nov 29, 2020 8:17 pm

Re: Loading and unloading a module dynamically at runtime

Post by MasterOfGizmo » Mon Nov 30, 2020 12:55 pm

kevinkk525 wrote:
Mon Nov 30, 2020 8:10 am
Try this: viewtopic.php?f=15&t=5302&hilit=unload+module
Thanks a lot. There was a small difference between my code and this example here. I was importing my script from a subdirectory and used

Code: Select all

	exec('from apps import ' + name, {} )
	sys.modules["apps."+name].myfunc()
	del sys.modules["apps."+name]
This wasn't working for a second time as explained. Second time the script was just not executed and didn't reappear in sys.modules

After reading the thread you linked to I tried:

Code: Select all

	exec('import  apps.' + name, {} )
	sys.modules["apps."+name].myfunc()
	del sys.modules["apps."+name]
And this code works. The script does run a second time.

Thanks a lot, although I don't fully understand the difference.

Post Reply