Re-import module?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
sw_dev
Posts: 7
Joined: Wed Nov 19, 2014 9:27 pm

Re-import module?

Post by sw_dev » Wed Nov 19, 2014 9:29 pm

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.)
Last edited by sw_dev on Thu Nov 20, 2014 8:17 pm, edited 1 time in total.

User avatar
dhylands
Posts: 3415
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Re-import module?

Post by dhylands » Wed Nov 19, 2014 9:34 pm

Currently, what I do is to Press Control-D which does a soft reset, and then re-import.

sw_dev
Posts: 7
Joined: Wed Nov 19, 2014 9:27 pm

Re: Re-import module?

Post by sw_dev » Wed Nov 19, 2014 9:40 pm

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?

sw_dev
Posts: 7
Joined: Wed Nov 19, 2014 9:27 pm

Re: Re-import module?

Post by sw_dev » Thu Nov 20, 2014 2:09 am

(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.

User avatar
pythoncoder
Posts: 4351
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Re-import module?

Post by pythoncoder » Thu Nov 20, 2014 9:55 am

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.
Peter Hinch

sw_dev
Posts: 7
Joined: Wed Nov 19, 2014 9:27 pm

Re: Re-import module?

Post by sw_dev » Thu Nov 20, 2014 7:19 pm

(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.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Re-import module?

Post by pfalcon » Thu Nov 20, 2014 10:35 pm

sw_dev, your posts are being held for moderator approval. Why still? Dunno, maybe forum doesn't like some TLAs ;-).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Re-import module?

Post by pfalcon » Thu Nov 20, 2014 10:48 pm

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).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

sw_dev
Posts: 7
Joined: Wed Nov 19, 2014 9:27 pm

Re: Re-import module?

Post by sw_dev » Thu Nov 20, 2014 10:55 pm

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...

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Re-import module?

Post by pfalcon » Thu Nov 20, 2014 11:12 pm

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.)
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply