← Back to archive

Repo of the Day

micropython/micropython

Published: Dec 17, 2023

Open repository ↗

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems

Summary

MicroPython is a lean implementation of Python 3.x written in C, designed to run on microcontrollers and other resource-constrained platforms. It implements Python 3.4 syntax (including async/await from 3.5 and selected later features) and ships with hardware-specific modules for peripherals such as GPIO, Timers, ADC, DAC, PWM, SPI, I2C, CAN, Bluetooth, and USB.

What it is useful for

  • Running Python scripts directly on microcontrollers like the ESP32, STM32, Raspberry Pi RP2040/RP2350, and Microchip SAMD21/SAMD51.
  • Prototyping embedded applications in Python instead of C, with targets supported down to 256kiB flash and 16kiB RAM. Devices with at least 512kiB flash and 128kiB RAM allow a more full-featured experience.
  • Building and testing MicroPython itself on Unix-like systems and Windows via the dedicated Unix and Windows ports, which can also serve as a lightweight Python alternative on embedded Linux.
  • Studying or creating a new microcontroller port, using the bare-arm or minimal ports as starting points.
  • Executing Python source from .py files or precompiled .mpy bytecode, the latter being frozen into firmware to reduce load time and storage.
  • Networking where supported: certain ports include socket, ssl, and asyncio, and some include the _thread module for multithreading.

How engineers can use it

The repository is split into py/ (core implementation), mpy-cross/ (cross-compiler), ports/ (per-target code), lib/, tests/, docs/, extmod/, tools/, and examples/. To build, you need bash, gcc, and Python 3.3+ available as python3; use gmake on BSD systems.

  1. Build the cross-compiler used to pre-compile Python to .mpy bytecode:
    cd mpy-cross
    make
    
  2. Initialise the third-party submodules required by your chosen port (vendors provide drivers and HALs as submodules):
    cd ports/<port-name>
    make submodules
    
  3. Build the firmware for the port with make. Note that the rp2 and esp32 ports additionally use CMake.
  4. Flash the resulting binary to your board and use the REPL or copy .py/.mpy files to the on-device filesystem.

Only a subset of Python 3 is implemented for the supplied data types and modules, and port maturity varies across the three support tiers documented in the README. For the API reference and board-specific quick-reference pages, the README points to the online documentation. Community help is available through GitHub Discussions and Discord. The README states the project is licensed under the MIT license, while the GitHub metadata lists the license as NOASSERTION, so the LICENSE file should be checked before redistribution.