How To Manage MicroPython Modules With Mip on Raspberry Pi Pico
The latest lightweight package manager for MicroPython
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.
Stay On the Cutting Edge: Get the Tom's Hardware Newsletter
Get Tom's Hardware's best news and in-depth reviews, straight to your inbox.
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 <command>: Use a series of file system commands with the MicroPython device. These commands are similar to common Unix / Linux commands.
Command | Description |
---|---|
cat | Shows the contents of a file |
ls | List the content of the current directory |
ls | List the contents of a given directory |
cp [-r] | Copy files. Use : prefix to specify a file on the MicroPython device. Recursive uses -r |
rm | Remove files from the device |
mkdir | Create a directory on the device |
rmdir | Delete a directory on the device |
touch | Create a file on the device using |
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
Python How Tos
- How To Install Python on Windows 10 and 11
- How to use For Loops in Python
- How to Enumerate in Python
- How to Create Executable Applications in Python
- How To Remove Backgrounds From Images With Python
- How to Create Web Apps with Python, HTML and Thonny
- How To Use Raspberry Pi Camera Module 3 with Python Code
Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".
AMD to release the Krackan at CES — CPU rivals Intel Lunar Lake in new benchmarks, Ryzen AI 7 350 takes on the Core Ultra 7 258V
Best Holiday Monitor Deals 2024: 4K, Gaming, and More
The world’s smallest arcade machine fits in the palm of your hand — Arduino microcontroller powers tiny Pong arcade machine