Notes about MicroPython

Documentation

Official firmwares

Issues tracker

Package installation

On a network-enabled device :

>>> import mip
>>> mip.install("package-name")

Using mpremote

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.

Install a package :

$ mpremote connect /dev/ttyUSB0 mip install package-name

Copy a file :

$ mpremote connect /dev/ttyUSB0 cp python-stdlib/base64/base64.py :/lib

Frozen modules

Definition by the officiel doc :

https://docs.micropython.org/en/latest/reference/glossary.html#term-frozen-module

A Python module that has been cross compiled and bundled into the firmware image. This reduces RAM requirements as the code is executed directly from flash.

Frozen modules are preprocessed (kind of pre-compiled) modules added to (baked-in) the MicroPython firmware of the device.

Freezing modules allow to reduce the memory footprint of these modules and to leave more memory available for the custom modules developed on top of the firmware.

This also allow to use big modules that could not be processed by the device's MicroPython interpreter.

Frozen modules also load faster because they are already processed and the MicroPython interpreter doesn't have to process them again.

SCRIPT_DIR

In MicroPython, SCRIPT_DIR is a built-in global variable that represents the directory where the current script is located. It is used to access files and other resources that are stored in the same directory as the script.

Which hardware am I running on?

There is no direct method for software written in MicroPython to discover whether it is running on a Raspberry Pi Pico or a Pico W by looking at the hardware. However, you can tell indirectly by looking to see if network functionality is included in your particular MicroPython firmware:

import network
if hasattr(network, "WLAN"):
    # the board has WLAN capabilities

Alternatively, you can inspect the MicroPython firmware version to check whether it was compiled for Raspberry Pi Pico or for Pico W using the sys module.

>>> import sys
>> sys.implementation
(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico W with RP2040', _mpy=4102)

So if the 'Pico W' string is present and in sys.implementation._machine that can be used to determine whether your firmware was compiled for Pico W.

Miscellaneous commands

>>> import os
>>> os.getcwd()
'/'

>>> os.listdir('/')
['lib', 'main.py']

>>> os.listdir('/lib')
['micropython_ssd1306-0.3.dist-info', 'ssd1306.py']

>>> import sys
>>> sys.implementation
(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico with RP2040', _mpy=4102)

>>> sys.path
['', '.frozen', '/lib']

Remote shell / rshell

$ rshell
Connecting to /dev/cu.usbmodem8401 (buffer-size 512)...
[...]
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.

/Users/jamesbond/sandboxes/pico-w> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.19.1-900-gfc4c47f7b on 2023-02-27; Raspberry Pi Pico W with RP2040
Type "help()" for more information.

Terminal

Under OS X, we can use the screen in the terminal.

screen /dev/tty.usbmodem1444101 -b115200

>>> print("hello")
hello

MicroPython REPL client

Useful resources on the web