Re-import module?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
dhylands
Posts: 3239
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Re-import module?

Post by dhylands » Thu Dec 17, 2015 8:43 pm

You can use __name__ on a module to get its name. It's not perfect. I see that CPython uses module.__spec__.name.

I created /flash/make_x.py:

Code: Select all

def incr():
    try:
        with open('/flash/x.py', 'r') as f:
            lines = f.readlines()
            fields = lines[0].split()
            old_x = int(fields[2])
            new_x = old_x + 1
    except OSError:
        new_x = 0
    with open('/flash/x.py', 'w') as f:
        print('Updating x to {}'.format(new_x))
        f.write('value = {}\n'.format(new_x))
        f.write("print('value = {}'.format(value))\n")
and use the following reload function in my /flash/boot.py:

Code: Select all

def reload(mod):
    import sys
    mod_name = mod.__name__
    del sys.modules[mod_name]
    return __import__(mod_name)
and this is the result:

Code: Select all

MicroPython v1.5.1-117-g57dcfa3-dirty on 2015-12-13; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> import make_x
>>> make_x.incr()
Updating x to 0
>>> import x
value = 0
>>> x.value
0
>>> make_x.incr()
Updating x to 1
>>> x = reload(x)
value = 1
>>> x.value
1
>>> make_x.incr()
Updating x to 2
>>> x.value
1
>>> x = reload(x)
value = 2
>>> x.value
2
>>>
Note that if some other module had imported x, it will still have a reference to the old module and not the reloaded one.

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

Re: Re-import module?

Post by pythoncoder » Fri Dec 18, 2015 10:27 am

Nice one!
Peter Hinch

EasyRider
Posts: 94
Joined: Wed Dec 30, 2015 8:17 am

Re: Re-import module?

Post by EasyRider » Tue Jul 12, 2016 2:29 am

Newbie question.
AttributeError: 'module' object has no attribute
On pyboard.
From Repl trying to import and execute a very simple function.

Very simple file A.py , saved in main root directory.

Code: Select all


def f():
	print("test")
import A, works OK.

from A import f, does not work
ImportError: cannot import name f
A.f(), does not work
AttributeError: 'module' object has no attribute 'f'
All of above works in Python 3.5.2

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

Re: Re-import module?

Post by dhylands » Tue Jul 12, 2016 4:56 am

I tried your test on my pyboard and it worked fine. Which version of firmware are you using?

Code: Select all

MicroPython v1.8.1-82-gd583526-dirty on 2016-06-21; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> 
>>> import A
>>> from A import f
>>> f()
f called
>>>  
where A.py contained:

Code: Select all

def f():
    print('f called')

EasyRider
Posts: 94
Joined: Wed Dec 30, 2015 8:17 am

Re: Re-import module?

Post by EasyRider » Tue Jul 12, 2016 6:22 am

Thanks Dave,

pyboard 1.1 , firmware 1.8.2

Deleted some other code that was running in main.py at startup.
Hard Reset.
Good now .
Will investigate why it upset the importing of modules :?

Regards
John

solarkraft
Posts: 1
Joined: Sun May 20, 2018 5:14 pm

Re: Re-import module?

Post by solarkraft » Sun May 20, 2018 7:12 pm

Thanks for the pointers! I have set up the following functions:

[code]
def unload(mod):
#removes module from the system
mod_name = mod.__name__

import sys
if mod_name in sys.modules:
del sys.modules[mod_name]

def run(mod):
#executes a module (python file without .py)
#import would only run it once
unload(mod)
__import__(mod.__name__)
[/code]

"run" reloads the module.

[code]
>>> import test
test.py runs!
>>> import test
>>> #nothing happens
>>>
>>> run(test)
test.py runs!
>>> run(test)
test.py runs!
[/code]

Post Reply