Page 1 of 1

Some use cases and small talk for the mpremote tool

Posted: Wed Aug 24, 2022 10:16 am
by Wind-stormger
I've tried a lot of different 3rd party micropython tools, some are Windows apps, some are VScode plugins.

But when I tried MicroPython's mpremote tool, I almost decided it was going to be my go-to tool. :D

I usually use it in conjunction with VScode. Its code highlighting, auto-completion, and auto-indentation are very useful. It is also easy to open other plugins at the same time. The important thing is that VScode is also a multi-platform IDE.

Maybe some minimalists, I think the mpremote tool will also be suitable, just use the system's terminal with any text editor, if you are a terminal command line veteran, you only need a terminal to do everything.

Docs : https://docs.micropython.org/en/latest/ ... emote.html

Github : https://github.com/micropython/micropyt ... s/mpremote

pypi : https://pypi.org/project/mpremote/

----------
Most of the use cases are pretty clear in the documentation, but I'm not a command line veteran, so I'll put some use cases I figured out here, maybe it will help some newbies like me.

cp

This is probably the most common command besides repl, used to copy files from local to device, or from device to local. Probably because I am not yet familiar with the terminal command syntax, at first I could not fully grasp the usage of the cp command from the several use cases in the documentation.

First create a clean temporary folder and write a main.py into it.

Code: Select all

print ("start")
for i in range(2):
    print(i)
print ("end")

Simplest use case, go to this folder path in terminal, copy file from local to device :

Code: Select all

mpremote connect COM1 cp main.py : 

mpremote connect COM1 cp ./main.py : 

mpremote connect COM1 cp ./main.py :main.py

mpremote connect COM1 cp main.py :main.py
The four commands achieve the exact same function.

After the ":" symbol, if you enter a filename, the file will be renamed to this filename when copied to the device.

Copy file from device to local :

Code: Select all

mpremote connect COM1 cp :main.py .

mpremote connect COM1 cp :main.py ./

mpremote connect COM1 cp :main.py ./main.py

mpremote connect COM1 cp :main.py main.py
The four commands achieve the exact same function.

Just use the "." or "./" achieve the same function. But if you want to rename the file, it must be "./", or not used at all.

The above is the operation performed in the relative path, that is, in the path where the current terminal is located.

Copying files in absolute paths is a little more complicated.

On Windows, use the absolute path to the file to copy from local to device:

Code: Select all

mpremote connect COM1 cp D:\temp\main.py :main.py
Copy file from device to local :

Code: Select all

mpremote connect COM1 cp :main.py D:\temp\main.py
At present, special attention should be paid to the fact that the target filename cannot be omitted in Windows!

Using mpremote 0.3.0 that has been released in pypi, you will get a similar error when you do the following on Windows PowerShell:

Code: Select all

PS D:\temp> mpremote connect COM1 cp D:\temp\main.py :        
cp D:\temp\main.py :D:\temp\main.py
PS D:\temp> mpremote connect COM1 ls
ls :
          66 D: emp\main.py
         139 boot.py
PS D:\temp> mpremote connect COM1 cat D: emp\main.py
cat :D:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] ENOENT
The filename on the device is renamed to its full Windows absolute pathname and cannot be read with the "ls" command normally.

In case you get the same error, follow the steps below to remove the incorrect file.

Code: Select all

PS D:\temp> mpremote connect COM1 cat D:\temp\main.py
cat :D:\temp\main.py
print ("start")
for i in range(2):
    print(i)
print ("end")
PS D:\temp> mpremote connect COM1 rm D:\temp\main.py 
rm :D:\temp\main.py
PS D:\temp> mpremote connect COM1 ls
ls :
         139 boot.py
Copy file from device to local absolute paths , but omit the target filename :

Code: Select all

PS D:\temp> mpremote connect COM1 cp :main.py D:\temp\        
cp :main.py D:\temp\
Traceback (most recent call last):
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\Scripts\mpremote.exe\__main__.py", line 7, in <module>
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\lib\site-packages\mpremote\main.py", line 569, in main
    do_filesystem(pyb, args)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\lib\site-packages\mpremote\main.py", line 334, in do_filesystem 
    pyboard.filesystem_command(pyb, args, progress_callback=show_progress_bar)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\lib\site-packages\mpremote\pyboard.py", line 594, in filesystem_command
    op(src, dest2, progress_callback=progress_callback)        
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python310\lib\site-packages\mpremote\pyboard.py", line 499, in fs_get     
    with open(dest, "wb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\temp\\'
In Linux, such as Ubuntu, copying a file from an absolute path can omit the target filename:

Code: Select all

mpremote connect /dev/ttyACM0 cp ~/temp/main.py :
mpremote connect /dev/ttyACM0 cp /home/wind/temp/main.py :

mpremote connect /dev/ttyACM0 cp :main.py ~/temp/
mpremote connect /dev/ttyACM0 cp :main.py /home/wind/temp/
( To Be Continued)

Re: Some use cases and small talk for the mpremote tool

Posted: Wed Aug 24, 2022 10:34 am
by jimmo
Thanks! I will go over your posts in detail and hopefully make some docs updates based on your suggestions.
Wind-stormger wrote:
Wed Aug 24, 2022 10:16 am
Just use the "." or "./" achieve the same function. But if you want to rename the file, it must be "./", or not used at all.
Damien literally sent a PR today to fix this.

When this is merged, the cp command will now behave like scp for all combinations of local and remote. (scp is the tool that the : notation is copied from). ./ will no longer have any effect.

https://github.com/micropython/micropython/pull/9091

Re: Some use cases and small talk for the mpremote tool

Posted: Wed Aug 24, 2022 11:07 pm
by bulletmark
Hmm, that PR is going to stuff up the semantics of my mpr wrapper. Did not anticipate that so may have to add a specific device to device copy command.

Re: Some use cases and small talk for the mpremote tool

Posted: Wed Aug 24, 2022 11:53 pm
by jimmo
Wind-stormger wrote:
Wed Aug 24, 2022 10:16 am
But when I tried MicroPython's mpremote tool, I almost decided it was going to be my go-to tool.
"almost" ? :)

Re: Some use cases and small talk for the mpremote tool

Posted: Thu Aug 25, 2022 5:50 am
by Wind-stormger
jimmo wrote:
Wed Aug 24, 2022 11:53 pm
"almost" ? :)
Just because I'm also looking around for some other possible tools to try out, the once-familiar tool "Thonny" still occasionally continues to be used.

Now I will transfer the post to GitHub Discussions immediately.

Re: Some use cases and small talk for the mpremote tool

Posted: Mon Sep 05, 2022 1:32 am
by Wind-stormger
Latest updates and discussions on this post have been moved to GitHub:
https://github.com/micropython/micropyt ... sions/9096