Re-import module?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
stijn
Posts: 365
Joined: Thu Apr 24, 2014 9:13 am

Re: Re-import module?

Post by stijn » Fri Nov 21, 2014 12:48 pm

pfalcon wrote:Feature you ask for is rarely needed
I beg to differ. Maybe it's just me but I'm used to compiled languages where before running you at least know you won't get syntax errors. With Python however I'm only a beginner and the amount of times I have to restart a complete session becasue of some syntax or other error in the code is frustrating and time consuming: for instance I have a script which talks with external hardware which takes 10 seconds to connect with. Which is unfortunate enough, but then I have to redo that for every stupid error made and there's lots of them :]

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
Can't really argue with that. It's more of a nice to have though I guess for some it's more welcome than for others.

Architekt
Posts: 10
Joined: Wed Oct 15, 2014 8:59 am

Re: Re-import module?

Post by Architekt » Fri Nov 21, 2014 2:27 pm

Hi stijn, I have some suggestions for you:

1) Use static source code checker like flake8. Modern source code editors usually has plugins/extensions to integrate them and check you code on save.

2) Use pyboard.py to run and re-run your scripts on pyboard.

3) Use REPL to live debug/change your code running inside pyboard.

If you learn these tools, they should help you develop more effectively.

stijn
Posts: 365
Joined: Thu Apr 24, 2014 9:13 am

Re: Re-import module?

Post by stijn » Fri Nov 21, 2014 2:38 pm

Hey thanks for the reply, but I'm running on a PC not the pyboard. However static source code checking might indeed come in handy, will try. Thanks for the tip!

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

Re: Re-import module?

Post by Damien » Sat Nov 22, 2014 12:04 am

Is re-importing a module when it changes even possible in CPython? I tried it but it didn't work for me.

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

Re: Re-import module?

Post by dhylands » Sat Nov 22, 2014 12:21 am

Damien wrote:Is re-importing a module when it changes even possible in CPython? I tried it but it didn't work for me.
I did it for plugins in another program. It would detect that the timestamp changed and re-import the plugin (which was in reality a module). I'll dig up the code. I think it used __import__ or something like that.

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

Re: Re-import module?

Post by pfalcon » Sat Nov 22, 2014 10:54 am

Damien wrote:Is re-importing a module when it changes even possible in CPython? I tried it but it didn't work for me.
Few of web microframeworks use that feature to allow "live" code editing. But that's usually done in special "development" mode and not enabled in "production" mode to save the cycles (which is important even for CPython when we speak of (tens of?) thousands of concurrent connections). So yeah, I doubt it's done automagically by CPython itself, rather requires app's involvement. So again, we should just enable people to do re-imports, triggering them would likely stay their own task.
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/

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Re-import module?

Post by kfricke » Mon Nov 24, 2014 10:27 am

hmho, personal development preferrences should not be expected to alter platform implementation.

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

Re: Re-import module?

Post by pythoncoder » Tue Nov 25, 2014 1:12 pm

I may be missing the point here but isn't the cPython feature under discussion imp.reload? This replaces the Python 2.x reload(module) syntax.
Peter Hinch

stijn
Posts: 365
Joined: Thu Apr 24, 2014 9:13 am

Re: Re-import module?

Post by stijn » Tue Nov 25, 2014 3:00 pm

That's indeed the case, see http://stackoverflow.com/a/13121908/128384 and comment on it for example

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

Re: Re-import module?

Post by dhylands » Tue Nov 25, 2014 4:59 pm

And the very end of that thread says that imp.reload() is deprecated and importlib.reload() is the right thing to do.
https://docs.python.org/3.4/library/imp ... lib.reload

Post Reply