Managing modules in Python is often handled via pip, the Python package manager which uses a repository provided by PyPi to list available Python modules. But what is there for MicroPython? There was upip, a micro version of pip, but now there is mip, the new, official lightweight package manager for MicroPython.
Mip is designed for all MicroPython devices, be they online or offline. Devices which can connect to the Internet can be used directly via the Python Shell while offline devices can use a tool, mpremote, to install modules from your computer.
In this how-to, fee we will show you how to use mip directly on a Raspberry Pi Pico W, then offline using a Raspberry Pi Pico and mpremote. We will also go through a few handy mpremote commands.
Using mip With the Raspberry Pi Pico W
Using mip with a network connected MicroPython device means that modules can be directly installed to the device in a similar means to pip installing Python modules, and package managers in Linux.
1. Follow these steps to download the latest version of MicroPython for the Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and to set up Thonny. The rest are optional. Ensure that you are downloading MicroPython 1.20 or newer.
2. Open Thonny and click on the Stop button to refresh the connection. This ensures that the Python Shell is open and working correctly.
3. Create a new file. This file will contain all of the steps necessary to connect to Wi-Fi.
4. Add the following lines of code to the new file. Change the SSID and PASSWORD to match your own.
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID","PASSWORD")
print(wlan.isconnected())
5. Save the file to the Raspberry Pi Pico W as network-connection.py
6. Click on Run to start a Wi-Fi connection. After a few seconds it should print True to the Python shell. This indicates that we have an Internet connection. If false, click Stop and then Run again.
7. Import mip, the lightweight package manager.
import mip
8. Test mip by installing a package. I chose umqtt, a MQTT module for MicroPython. Packages are installed by calling mip’s install function and passing it the name of a package. Mip uses micropython-lib as its index, Python 3’s package manage, pip uses the PyPI index.
mip.install(“umqtt.simple”)
9. Test installing a third-party MicroPython package. Mip can also be used to install third-party packages outside of the micropython-lib index. Here we pass the install function the URL for the PicoZero library from the Raspberry Pi Foundation.
mip.install(“https://raw.githubusercontent.com/RaspberryPiFoundation/picozero/master/picozero/picozero.py”)
Using Mip With Mpremote on Raspberry Pi Pico
For MicroPython on a device with no network access, a Raspberry Pi Pico, mip will need to be used with mpremote, a tool that will communicate with the device over a USB / serial interface.
1. Follow these steps to download the latest version of MicroPython for the Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and to set up Thonny. The rest are optional. Ensure that you are downloading MicroPython 1.20 or newer.
2. Ensure that Python 3 is installed on your machine.
3. Open a Command Prompt and use pip to install mpremote.
pip install mpremote
4. Run mpremote and pass mip as an argument, and then specify the package name or the URL for the module. Here I am installing a package to use seven segment displays with the Pico.
mpremote mip install https://raw.githubusercontent.com/mcauser/micropython-tm1637/master/tm1637.py
Other Useful mpremote Commands
Mpremote is a useful tool for quick tasks on a MicroPython device. We’ve detailed a few extra useful commands that will help manage a MicroPython device.
mpremote: Automatically connects to a device running MicroPython to view the output of running code. Press CTRL + ] to close the connection.
mpremote repl: Opens an interactive Python shell, a REPL (Read, Eval, Print, Loop) where a user can directly work with the hardware.
mpremote soft-reset: Reboot the attached MicroPython device. This is the same as pressing CTRL + D in the REPL.
mpremote fs
In the example we list the contents of the flash storage, create a new file, then re-list the storage to see the new file.
MORE: Best RP2040 Boards
MORE: Best Raspberry Pi Projects
MORE: Raspberry Pi: How to Get Started