"asking" micropython to run a script written on the device from the REPL or the WebREPL #10145
-
How to simply run a micropython script which is recorded on the device? Of course when it is available and executable on the LOCAL HOST environment, one can use "mpremote". Some people do copy the toto.py script as the main.py and reboot..... Some other people propose to "import" the script as it would be a library. Then , as a feature of python library, any code outside any function declaration will be executed at load time, ONCE. Re-importing will not do any thing! So what I lack is the equivalent of having a local "python" command stating that the next parameter on the line is the name of the file containing things to be interpreted. Or adding a directive at the first line of the script to tell that it needs to be in an environment with python interpreter. But here, the interpreter is always there, and does not need to be called, we are in the micropython REPL! . aren't we? So I might have missed something.The solution I found, in order to only use Micropython interpreter is to use exec() , like in the next sequence, in order to run 'toto.py':
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
To "reload" / be able to run it again at REPL/WebREPL:
|
Beta Was this translation helpful? Give feedback.
-
So I went one step ahead and i tried to write a python library (= module) with the definition of mp() function: # library basic version
# exec parameter is the whole script text in one string
def mp(script_name):
fid1=open(script_name)
toto=fid1.read()
fid1.close()
exec(toto)
print('the basic mp_lib is imported')
#that's all , folks calling mp() on simple script works fine,
it works fine, even por several levels of loop in the program but as soon as there are internal function in the script to be excuted, basic example1: #test_scope1.py
def f1(x):
y=x+foo
return(y)
foo=4
a1=f1(3)
a2=f1(a1)
print(a2)
as one can read "name 'foo' is not defined. #test_scope3.py
def f1(x):
foo=6
y=x+foo
return(y)
# foo=4
a1=f1(3)
a2=f1(a1)
print(a2)
This is very annoying. For instance if you import the machine library within your code, you can not use it within a function, or you need to import it inside the definition, and it will be (partly) executed at evey call! any idea? |
Beta Was this translation helpful? Give feedback.
-
Sorry for the bad indentations for the scripts! # library basic version
# exec parameter is the whole script text in one string
def mp(script_name):
fid1=open(script_name)
toto=fid1.read()
fid1.close()
exec(toto)
print('the basic mp_lib is imported')
#that's all , folks |
Beta Was this translation helpful? Give feedback.
@bonne-soeur
To "reload" / be able to run it again at REPL/WebREPL:
import
will checksys.modules
dict and iftoto
is there it won't load it, so basically just delete it from there and it can be imported again 👍🏼 .