micropython-gestion-librerias-mip-upip

Managing Libraries in MicroPython with mip and upip

  • 5 min

A library in MicroPython is an external module we add to the board to reuse existing code.

One of Python’s great advantages is its immense library ecosystem. In MicroPython, the philosophy is the same, albeit with nuances. The base firmware includes essential modules, but sooner or later we’ll need drivers for a specific sensor, a database client, or encryption tools.

That’s where package managers come in. Their function is simple: download code written by others and place it on our board so we can use it with a simple import.

Historically, upip was used, but since version 1.19 we have a much more powerful and optimized tool called mip.

MicroPython libraries are typically installed in the /lib folder of the internal memory. MicroPython automatically adds this path to its sys.path, so it will find any module we store there.

The Modern Standard: mip

The mip (MicroPython Installer for Packages) manager is the currently recommended tool. Unlike its predecessor, mip is designed with the limited resources of microcontrollers in mind.

Its main advantages are:

  1. Downloads optimized versions (byte-code .mpy) when possible, saving RAM and storage.
  2. Allows installing directly from GitHub or GitLab.
  3. Works both from the board itself (if it has WiFi) and from the computer (via mpremote).

Installation from the Board (Requires Wi-Fi)

If we have a device with connectivity, such as an ESP32 or a Raspberry Pi Pico W, and we’ve already connected it to our WiFi network, we can install packages directly from the REPL.

First, we import the module and execute the installation:

import mip

# Install an official package (e.g., a sensor driver)
mip.install("ssd1306")

# Install 'umqtt.simple' for MQTT
mip.install("umqtt.simple")
Copied!

What happens “under the hood” is that mip queries the official micropython-lib index (the central library repository), downloads the necessary files, and places them neatly in the /lib folder of your device.

Installing from GitHub

This is one of the features we like the most. If we find an interesting library on GitHub that isn’t in the official index, we can install it directly by pasting the URL:

import mip
mip.install("github:org/user/repo")
Copied!

mip is smart enough to look for a package.json file in the repository and figure out which specific files to download.

Installation from the PC using mpremote

What if we have a regular Raspberry Pi Pico (without WiFi)? Or if we simply don’t want to configure the network right now?

This is where mpremote (which we saw in the previous chapter) shines again. We can use the USB connection so our computer acts as a “bridge”, downloads the library from the internet, and injects it into the board.

From your computer’s terminal (not the REPL):

mpremote mip install ssd1306
Copied!

This command uses your PC’s internet connection to look for the ssd1306 package and automatically copies it to the /lib folder of the connected microcontroller. It’s fast, clean, and doesn’t consume board memory during the process.

The Legacy: upip

Before mip, the standard was upip (MicroPython pip). It’s a stripped-down version of the pip we use in desktop Python.

Although it’s considered obsolete in modern MicroPython versions, you can still find it in older tutorials or on firmwares for the ESP8266, where mip isn’t always available by default due to memory constraints.

To use it (requires WiFi connection on the board):

import upip
upip.install('micropython-pystone')
Copied!

upip downloads complete .py source files and generally requires more RAM to perform the installation (due to SSL handling and decompression). If you have a choice, use mip.

Manual Installation

Sometimes technology fails, or we’re simply developing our own library. In these cases, the manual method is foolproof.

Manually installing a library is simply copying the .py file into the /lib folder of the board.

We can do this with Thonny IDE:

  1. Download the .py file (e.g., my_library.py) on your computer.
  2. Open Thonny and connect your board.
  3. Right-click on the file in the local file explorer -> “Upload to /lib” (or drag and drop if your Thonny version allows it).
  4. If the /lib folder doesn’t exist, you can create it yourself.

Which Installation Method to Choose

Library management has matured significantly. To choose without getting confused, you can follow this workflow:

  1. Are you on your PC and using the command line? Use mpremote mip install package_name. It’s the most convenient and works for any board (with or without WiFi).
  2. Are you programming on the board (ESP32/Pico W) and have WiFi? Use import mip and then mip.install().
  3. Is it your own library or a loose file? Copy and paste it into the /lib folder using Thonny.

With this, we have the tools to avoid writing all the code from scratch. In the following sections, we’ll start using these libraries to control real hardware.