how to execute "OTHER" python scripts

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
yk22wong
Posts: 10
Joined: Tue Dec 26, 2017 11:25 am

how to execute "OTHER" python scripts

Post by yk22wong » Tue Dec 26, 2017 11:42 am

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.

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

Re: how to execute "OTHER" python scripts

Post by dhylands » Wed Dec 27, 2017 12:07 am

If your file is called foo.py then from the REPL, you can type

Code: Select all

import foo
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.

yk22wong
Posts: 10
Joined: Tue Dec 26, 2017 11:25 am

Re: how to execute "OTHER" python scripts

Post by yk22wong » Wed Dec 27, 2017 10:01 am

Hi Dave,

That works. Million thanks.

yk22wong
Posts: 10
Joined: Tue Dec 26, 2017 11:25 am

Re: how to execute "OTHER" python scripts

Post by yk22wong » Thu Mar 29, 2018 8:11 am

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?

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: how to execute "OTHER" python scripts

Post by OutoftheBOTS_ » Thu Mar 29, 2018 8:39 am

I add this to my boot.py

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)
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)

yk22wong
Posts: 10
Joined: Tue Dec 26, 2017 11:25 am

Re: how to execute "OTHER" python scripts

Post by yk22wong » Thu Mar 29, 2018 8:48 am

Hi OotB,

Many thanks for sharing your idea. I'll try this out tonight.

regards
YK

yk22wong
Posts: 10
Joined: Tue Dec 26, 2017 11:25 am

Re: how to execute "OTHER" python scripts

Post by yk22wong » Thu Mar 29, 2018 9:23 am

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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: how to execute "OTHER" python scripts

Post by deshipu » Thu Mar 29, 2018 9:58 am

Or you can just press ctrl+d to do a soft reset.

User avatar
benalb
Posts: 25
Joined: Fri May 19, 2017 1:23 pm

Re: how to execute "OTHER" python scripts

Post by benalb » Thu Mar 29, 2018 2:39 pm

I usually do this:

Code: Select all

cat test01.py                                                                                                                                 

def my_test():
    print("hello, test")

def main():
    my_test()

if __name__ == "__main__":
    main()
and then, can do:

Code: Select all

>>> import test01
>>> from test01 import my_test
>>> 
>>> test01.my_test()
hello, test
>>> test01.main()
hello, test
>>> 
>>> my_test()
hello, test

yk22wong
Posts: 10
Joined: Tue Dec 26, 2017 11:25 am

Re: how to execute "OTHER" python scripts

Post by yk22wong » Thu Mar 29, 2018 2:57 pm

deshipu wrote:
Thu Mar 29, 2018 9:58 am
Or you can just press ctrl+d to do a soft reset.
Yes, I thought of this after using the reload method. Thanks

Post Reply