Page 4 of 4

Re: Re-import module?

Posted: Thu Dec 17, 2015 8:43 pm
by dhylands
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.

Re: Re-import module?

Posted: Fri Dec 18, 2015 10:27 am
by pythoncoder
Nice one!

Re: Re-import module?

Posted: Tue Jul 12, 2016 2:29 am
by EasyRider
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

Re: Re-import module?

Posted: Tue Jul 12, 2016 4:56 am
by dhylands
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')

Re: Re-import module?

Posted: Tue Jul 12, 2016 6:22 am
by EasyRider
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

Re: Re-import module?

Posted: Sun May 20, 2018 7:12 pm
by solarkraft
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]