how to execute "OTHER" python scripts
how to execute "OTHER" python scripts
Hi everyone, I have to admit I am new to Pyboard so if my question does not make sense in the PYB env, please feel free to correct me.
I got my Pyb a couple of months ago. Hardware, REPL,....everything works fine. Perfect, no complaint. My question is I do not know how to run a python script in the board if I do not want to name the script main.py.
Many thanks in advance for any advice.
By the way, I did some search in the forum as well as on the uPython website, I can't find anything that related to my question. If I have overlooked, my sincere apology.
I got my Pyb a couple of months ago. Hardware, REPL,....everything works fine. Perfect, no complaint. My question is I do not know how to run a python script in the board if I do not want to name the script main.py.
Many thanks in advance for any advice.
By the way, I did some search in the forum as well as on the uPython website, I can't find anything that related to my question. If I have overlooked, my sincere apology.
Re: how to execute "OTHER" python scripts
If your file is called foo.py then from the REPL, you can type That's how I normally test things. Then, when I'm happy everything is working properly, I put the 'import foo' line into main,py.
Code: Select all
import foo
Re: how to execute "OTHER" python scripts
Hi Dave,
That works. Million thanks.
That works. Million thanks.
Re: how to execute "OTHER" python scripts
A little bit of follow up. The “import myfile” method works. But I recently notice that it only works if that is the first time I do the import in that terminal session. If I type “import myfile” again after its first successful run, I will not see the Python script run again. There is no error shown also, it just gives me a new REPL prompt.
I think it is normal and it has to do with the meaning of “import”. So right now if I want to run myfile again, I have to exit the picocom as well as the Ubuntu terminal session and come back in again.
Just wonder if there is any workaround less clumsy than mine?
I think it is normal and it has to do with the meaning of “import”. So right now if I want to run myfile again, I have to exit the picocom as well as the Ubuntu terminal session and come back in again.
Just wonder if there is any workaround less clumsy than mine?
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: how to execute "OTHER" python scripts
I add this to my boot.py
First time that I want to run a file I will use import file_name then anytime after that I want to run it I use reload(file_name)
Code: Select all
import gc
from sys import modules
def reload(mod):
mod_name = mod.__name__
del sys.modules[mod_name]
gc.collect()
return __import__(mod_name)
Re: how to execute "OTHER" python scripts
Hi OotB,
Many thanks for sharing your idea. I'll try this out tonight.
regards
YK
Many thanks for sharing your idea. I'll try this out tonight.
regards
YK
Re: how to execute "OTHER" python scripts
Confirmation to OotB, your method works. Exactly as I needed. A big thank you.
However I need to change your second line to "import sys" in my env.
However I need to change your second line to "import sys" in my env.
Re: how to execute "OTHER" python scripts
Or you can just press ctrl+d to do a soft reset.
Re: how to execute "OTHER" python scripts
I usually do this:
and then, can do:
Code: Select all
cat test01.py
def my_test():
print("hello, test")
def main():
my_test()
if __name__ == "__main__":
main()
Code: Select all
>>> import test01
>>> from test01 import my_test
>>>
>>> test01.my_test()
hello, test
>>> test01.main()
hello, test
>>>
>>> my_test()
hello, test