Page 1 of 4

Re-import module?

Posted: Wed Nov 19, 2014 9:29 pm
by sw_dev
You've written a module. You run it, and it's not quite right. Now, is there any way to re-import it instead of rebooting the board?

Hmmm, the responses show a basic misunderstanding about the question, and for some reason I can't seem to post replies to this thread. Please note that the point of the exercise isn't to just re-import, but to re-import WITHOUT REBOOTING and pick up changes. (Ctrl-D is rebooting.)

Re: Re-import module?

Posted: Wed Nov 19, 2014 9:34 pm
by dhylands
Currently, what I do is to Press Control-D which does a soft reset, and then re-import.

Re: Re-import module?

Posted: Wed Nov 19, 2014 9:40 pm
by sw_dev
That's what I've been doing, but it occurs to me that python on other systems allows modules to be re-imported (Based on timestamp), so it might be available here, too.

Or, if it isn't, perhaps it should be?

Re: Re-import module?

Posted: Thu Nov 20, 2014 2:09 am
by sw_dev
(My response obviously got lost somewhere, I'll try to re-accomplish it.)

Note I said "without rebooting". The desktop python environment does this by checking file creation time to see if it needs to re-interpret the file, perhaps micropython can do something like that.

Re: Re-import module?

Posted: Thu Nov 20, 2014 9:55 am
by pythoncoder
This is a minor hassle because the REPL command history is lost. I have a workround: I hacked miniterm to send macros from a file to the Pyboard, but it's a kludge. It would be great if there were some way of retaining the REPL history. I considered doing it at the terminal end, but after some thought concluded it was a nontrivial exercise.

Re: Re-import module?

Posted: Thu Nov 20, 2014 7:19 pm
by sw_dev
(Apparently my replies are being deleted... WTF?)

There's some misunderstanding here. The idea is to NOT, repeat NOT REBOOT, and be able to re-import an edited module. The steps involved should be as follows:

1. Module X gets imported.
2. A deficiency is found with module X.
3. Module X's code is edited.
4. Through some means, module X is re-imported WITHOUT REBOOTING.

Re: Re-import module?

Posted: Thu Nov 20, 2014 10:35 pm
by pfalcon
sw_dev, your posts are being held for moderator approval. Why still? Dunno, maybe forum doesn't like some TLAs ;-).

Re: Re-import module?

Posted: Thu Nov 20, 2014 10:48 pm
by pfalcon
sw_dev, regarding what you suggest, it's understood, and currently, it's not supported. MicroPython cannot behave in all aspects like CPython, or it won't be MicroPython any longer. Feature you ask for is rarely needed, but adds noticeable overhead to implementation - that's usually a rule for "skip this feature" we used so far. Feel free to argue for a usecase when it's unavoidably needed though. Otherwise, the suggestion is to do the same as you do with CPython - run your script from shell command line again (which for PyBoard translates to fast-reboot to start that new interpreter instance).

Re: Re-import module?

Posted: Thu Nov 20, 2014 10:55 pm
by sw_dev
pfalcon,
It's like adding a compile/link step to an interpreted environment. Silly, when a new command ("reimport", maybe?) could be introduced if the time/date check was considered too ponderous.

OTOH, I suppose I could just read the file from disk, and eval/exec it. (Yuck!)

Oh well...

Re: Re-import module?

Posted: Thu Nov 20, 2014 11:12 pm
by pfalcon
We cannot introduce arbitrary "commands" on a whim, or risk ending up as being non-Python. The closest (and Pythonic) solution along the way you suggest would be to expose sys.modules and let user .clear() it. But we have bigger (but still rare) problems with imports, e.g. https://github.com/micropython/micropython/issues/967 . So, unless someone starts pumping code for that, it'll have to wait. (But feel free to submit feature request in the tracker.)