PC python script to run mpremote commands

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

PC python script to run mpremote commands

Post by PM-TPI » Mon Jun 06, 2022 9:01 pm

So....

Code: Select all

from mpremote import main
main.main()
returns...
Connected to MicroPython at COM12
Use Ctrl-] to exit this shell


But how do I run say... the 'mpremoyte ls' command ?
main() takes 0 positional arguments... so how do I pass 'ls' ?

I don't want to use bash scripts.

jahr
Posts: 11
Joined: Fri Apr 02, 2021 1:20 pm

Re: PC python script to run mpremote commands

Post by jahr » Tue Jun 07, 2022 8:14 am

Hello,

I'm using it this way

Code: Select all

  def _call_mpremote(self, args):
    args.insert(0, 'mpremote')
    proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    try:
      out, err = proc.communicate(timeout=None if self._timeout in (None, 0) else self._timeout)
# etc.
where args are arguments of mpremote.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: PC python script to run mpremote commands

Post by PM-TPI » Tue Jun 07, 2022 9:53 am

jahr

Can't we make calls to mpremote directly
without needing to go back out to OS

jahr
Posts: 11
Joined: Fri Apr 02, 2021 1:20 pm

Re: PC python script to run mpremote commands

Post by jahr » Tue Jun 07, 2022 12:33 pm

PM-TPI wrote:
Tue Jun 07, 2022 9:53 am
jahr

Can't we make calls to mpremote directly
without needing to go back out to OS
Maybe. I've tried and did not succeeded. IMHO mpremote is not intended to be called as a module and pyboard.py should be used instead. For my use case, the method described was sufficient.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: PC python script to run mpremote commands

Post by PM-TPI » Tue Jun 07, 2022 1:02 pm

jahr wrote:
Tue Jun 07, 2022 12:33 pm
IMHO mpremote is not intended to be called as a module
https://www.youtube.com/watch?v=EVJA01W9ArI
219 views Oct 26, 2021 Damien George instructs us on how to use mpremote

look at 1min 28 sec
Damien didn't go into any detail in using it as a module.

Thank you jahr... subprocess, learn something new everyday.

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

Re: PC python script to run mpremote commands

Post by dhylands » Wed Jun 08, 2022 6:57 pm

You could also just populate sys.argv and then call main.main()

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: PC python script to run mpremote commands

Post by jimmo » Mon Jun 20, 2022 6:20 am

PM-TPI wrote:
Mon Jun 06, 2022 9:01 pm
But how do I run say... the 'mpremoyte ls' command ?
mpremote is built on pyboard.py, and this is designed to be used as a module from your own scripts.
See https://docs.micropython.org/en/latest/ ... rd-library

You can use pyb.fs_ls(), however this will print out to stdout which is probably not what you want. It would be good to extend these libraries to support this use case.

Post Reply