Many people control their Pi Pico using Thonny but I rarely use the Desktop and prefer to program from the command line.
How can I control micropython on a Pico from the command line?
Many people control their Pi Pico using Thonny but I rarely use the Desktop and prefer to program from the command line.
How can I control micropython on a Pico from the command line?
The mpremote command line tool provides an integrated set of utilities to remotely interact with, manage the filesystem on, and automate a MicroPython device over a serial connection.
There is documentation for mpremote but it is not specific to the Pi or Pico.
The first step is to install mpremote and this needs to be installed into a virtual environment.
The following is the procedure to install;
I use the hidden directory ~/.local/myenv but you can use any location.
mkdir ~/.local/myenv && cd .local && python -m venv --system-site-packages myenv
source ~/.local/myenv/bin/activate
pip install --user mpremote
When completed run deactivate
Ensure ~/.local/bin is on your path
The simplest way to use this tool is just by invoking it without any arguments: mpremote.
mpremote automatically detects and connects to the first available USB serial device and provides an interactive terminal that you can use to access the REPL and your program’s output.
Serial ports are opened in exclusive mode, so running a second (or third, etc) instance of mpremote will connect to subsequent serial devices, if any are available.
More usefully mpremote supports being given a series of commands at the command line (see link above) which will perform various actions in sequence on a remote Pico.
This documentation provides details and other examples and is worthwhile studying but can be confusing at first. Simple examples are listed below.
The following examples all use LedTimer.py stored on the Pi (or other controlling computer) but you can use any micropython program.
To run a program on the Pico use mpremote run LetTimer.py
This will execute the file (stored on your Pi) directly from RAM on the Pico without copying it to the filesystem.
mpremote fs ls
mpremote fs cp LedTimer.py :LedTimer.py
This can also be used to upload library code to be imported.
Connect to REPL mpremote then
import LedTimer
mpremote rtc --set
Will set the Pico RTC to the host PC's current time.
mpremote fs cat LedTimer.py
mpremote automatically detects and connects to the first available USB serial device but if you have multiple devices you can specify which (by prepending an alias to the command)
e.g. mpremote a0 fs ls
There are a number of predefined aliases.
You can create your own aliases e.g. to specify a specific Pico.
Identify the device with mpremote connect list then create a file .config/mpremote/config.py with contents containing the Pico id similar to:-
commands = {
"p0": "connect id:e66038b7130c4230",
"p2": "connect id:5af27ed7d3cffe4a"
}