# MicroPython

**MicroPython** 是 [Python](https://www.edgepedia.org/zh/python-programming-language) 3 编程语言的一种精简、高效的实现，包含 Python 标准库的一个小子集，旨在运行于微控制器及其他资源受限环境<sup>[1](https://micropython.org/?lang=en)</sup>。它是一个完整的编译器和运行时，直接在裸机上执行，提供交互式 REPL、内置文件系统以及 `machine` 等硬件访问模块，其紧凑程度足以在 256 KB 代码空间和 16 KB 内存中容纳并运行<sup>[1](https://micropython.org/?lang=en)</sup>。该项目由软件工程师达米恩·乔治（Damien George）从零编写实现，最初通过 2013 年 11 月 13 日发起的一场 [Kickstarter](https://www.edgepedia.org/zh/kickstarter) 众筹获得资助，筹资目标为 15,000 英镑<sup>[2](https://micropython.org/resources/MicroPython10YearsPoster.pdf)</sup><sup> • </sup><sup>[3](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 创建者与起源 | 达米恩·乔治；Kickstarter 于 2013 年 11 月 13 日发起，筹资目标 15,000 英镑<sup>[2](https://micropython.org/resources/MicroPython10YearsPoster.pdf)</sup><sup> • </sup><sup>[3](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)</sup> |
| 资源占用 | 在 256 KB 代码空间和 16 KB 内存内运行；pyboard 硬件：STM32F405RG，168 MHz Cortex-M4，1,024 KiB 闪存，192 KiB 内存<sup>[1](https://micropython.org/?lang=en)</sup> |
| 许可证与语言 | 以 C99 编写；核心以 MIT 许可证发布<sup>[1](https://micropython.org/?lang=en)</sup> |
| 移植版本 | 二十多个移植版本，分属不同支持层级；第 1 层级包括 esp32、mimxrt、rp2、samd、stm32、unix 和 windows<sup>[4](https://github.com/micropython/micropython)</sup> |
| 硬件访问 | `machine` 模块暴露 GPIO、PWM、ADC、I2C 和 SPI 等底层硬件<sup>[1](https://micropython.org/?lang=en)</sup><sup> • </sup><sup>[5](https://learn.adafruit.com/micropython-basics-what-is-micropython/overview?view=all)</sup> |
| 与 C 的性能对比 | 在 ESP32 和 Raspberry Pi Pico 上的基准测试显示，I/O 比 C 慢 10 至 20 倍，内存占用高 4 至 6 倍，足以胜任常见的传感器和网络任务<sup>[6](https://arxiv.org/abs/2608.18160)</sup> |
| 著名分支 | CircuitPython，Adafruit 面向初学者打造的分支；超过 95% 的 MicroPython 代码可在其上不加修改地运行，但硬件 API 有所不同<sup>[7](https://pythonresources.com/guides/circuitpython-vs-micropython/)</sup> |
| 当前版本 | 1.29.0 版源码发行包<sup>[8](https://micropython.org/download/)</sup> |

## 历史

乔治利用业余时间启动了该项目，据他在 2013 年 11 月 Kickstarter 众筹发起后不久的一次采访中所述，当时他已投入超过 700 小时的工作<sup>[3](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)</sup>。该实现包括完整的解析器、编译器、虚拟机、运行时系统和垃圾回收器，全部从零编写<sup>[3](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)</sup>。解析器将 Python 语法编码为一种静态数据结构，由一个 320 行的非递归函数解释执行；编译器既能生成字节码，也能生成原生 Thumb 机器码<sup>[3](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)</sup>。

众筹资助了 pyboard——MicroPython 的官方开发板，围绕 STM32F405RG 微控制器构建，配备 168 MHz Cortex-M4 CPU、1,024 KiB 闪存和 192 KiB 内存<sup>[1](https://micropython.org/?lang=en)</sup>。2016 年 3 月的第二场众筹从 1,384 名支持者处筹集了 28,334 英镑，用于以软件为重点的工作<sup>[2](https://micropython.org/resources/MicroPython10YearsPoster.pdf)</sup>。乔治因对 MicroPython 的持续开发于 2017 年获得谷歌开源软件奖<sup>[2](https://micropython.org/resources/MicroPython10YearsPoster.pdf)</sup>。

## 架构

**编译器。** MicroPython 通过四遍将全部代码编译为字节码或原生代码：作用域、栈大小、代码大小和发射，每一遍都在同一抽象语法树上运行相同的 C 代码<sup>[9](https://micropython.org/resources/docs/en/latest/develop/compiler.html)</sup>。字节码发射器是默认选项；原生发射器直接面向机器码，内联汇编器支持 Thumb 和 Xtensa 指令集<sup>[9](https://micropython.org/resources/docs/en/latest/develop/compiler.html)</sup><sup> • </sup><sup>[1](https://micropython.org/?lang=en)</sup>。交叉编译器 `mpy-cross` 可将 Python 脚本预编译为 `.mpy` 文件，这些文件可固化到固件中，因此预编译脚本除其创建的动态对象外不占用内存<sup>[4](https://github.com/micropython/micropython)</sup><sup> • </sup><sup>[1](https://micropython.org/?lang=en)</sup>。

**内存管理。** 堆内存由一个简单、快速的标记-清除垃圾回收器管理；堆耗尽时会引发 `MemoryError`<sup>[1](https://micropython.org/?lang=en)</sup>。可随时通过 `gc.collect()` 请求回收，分配失败或空闲内存低于可配置阈值时会自动触发回收<sup>[10](https://docs.micropython.org/en/latest/reference/constrained.html)</sup>。一次回收可能耗时数毫秒，但在 Pyboard 上工作量较小时约需 1 毫秒<sup>[10](https://docs.micropython.org/en/latest/reference/constrained.html)</sup>。碎片化是一个实际问题：如果一个长生命周期对象分配在短生命周期对象之上，回收的内存只能用于不大于被释放对象大小的对象<sup>[10](https://docs.micropython.org/en/latest/reference/constrained.html)</sup>。

**语言特性。** MicroPython 实现 Python 3.4 语法，包含交互式提示符、任意精度整数、闭包、列表推导式、生成器和异常处理<sup>[1](https://micropython.org/?lang=en)</sup><sup> • </sup><sup>[3](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)</sup>。它支持指针标记，可将小整数、字符串和对象装入一个机器字；支持 30 位填充浮点数，无需堆内存；还通过 `_thread` 模块支持多线程，配有可选的全局解释器锁，仅在部分移植版本上可用<sup>[1](https://micropython.org/?lang=en)</sup>。

## 移植版本与支持层级

移植版本由位于虚拟机之下、面向特定芯片系列的 C 代码构成，负责启动、内存布局、USB 或 UART 支持以及 `machine` 模块；而开发板则在该移植版本内定义引脚布局和一组编译选项<sup>[11](https://micropython.com/language/ports/)</sup>。代码仓库提供二十多个移植版本，分属项目的不同支持层级<sup>[4](https://github.com/micropython/micropython)</sup>。

第 1 层级移植版本成熟、得到积极维护、每次发版均在硬件上测试，并有移植版本专属文档：esp32（乐鑫 ESP32、S2、S3、C3 和 C6 SoC）、mimxrt（恩智浦 i.MX RT）、rp2（树莓派 RP2040 和 RP2350）、samd（Microchip SAMD21 和 SAMD51）、stm32（意法半导体多种 MCU）、unix 和 windows<sup>[4](https://github.com/micropython/micropython)</sup><sup> • </sup><sup>[12](https://micropython.org/resources/docs/en/latest/develop/support_tiers.html)</sup>。esp32 移植版本获得其厂商的持续资金支持<sup>[4](https://github.com/micropython/micropython)</sup>。第 2 层级移植版本包括 alif、embed、nrf、psoc-edge、renesas-ra、webassembly 和 zephyr，成熟度较低但获得完整支持，具有稳定的 Python API，发版时进行硬件测试<sup>[4](https://github.com/micropython/micropython)</sup><sup> • </sup><sup>[12](https://micropython.org/resources/docs/en/latest/develop/support_tiers.html)</sup>。第 3 层级移植版本（cc3200、esp8266、pic16bit）在 CI 中构建，但维护者不定期测试；第 M 层级移植版本（bare-arm、minimal、qemu）用于维护、开发和测试<sup>[4](https://github.com/micropython/micropython)</sup><sup> • </sup><sup>[12](https://micropython.org/resources/docs/en/latest/develop/support_tiers.html)</sup>。

unix 和 windows 移植版本允许开发和测试 MicroPython 本身，并提供 CPython 的轻量替代方案，尤其在嵌入式 Linux 系统上<sup>[4](https://github.com/micropython/micropython)</sup>。核心虚拟机和运行时没有外部依赖，但特定移植版本可能依赖第三方驱动或厂商硬件抽象层<sup>[4](https://github.com/micropython/micropython)</sup>。

## CircuitPython 与生态系统

**CircuitPython** 是 Adafruit 对 MicroPython 的开源分支，旨在简化在低成本微控制器上的实验和教学<sup>[5](https://learn.adafruit.com/micropython-basics-what-is-micropython/overview?view=all)</sup><sup> • </sup><sup>[13](https://www.adafruit.com/circuitpython)</sup>。超过 95% 的 MicroPython 代码可在 CircuitPython 上基本不加修改地运行，因为底层语言相同；不同之处在于硬件 API 层，CircuitPython 使用 `digitalio` 等统一 API，没有 `machine` 模块<sup>[7](https://pythonresources.com/guides/circuitpython-vs-micropython/)</sup><sup> • </sup><sup>[14](https://docs.circuitpython.org/en/stable/docs/)</sup>。CircuitPython 在大多数开发板上启用原生 USB，因此可通过拖放在 CIRCUITPY 驱动器上编辑文件；它禁用中断和线程，而 MicroPython 支持这些功能<sup>[14](https://docs.circuitpython.org/en/stable/docs/)</sup>。

生态系统调查显示，MicroPython 支持超过 200 种开发板，CircuitPython 支持超过 400 种，教育是主要的采用驱动力<sup>[6](https://arxiv.org/abs/2608.18160)</sup>。项目自带的测试套件包含超过 590 个测试、逾 18,500 个独立测试用例，核心代码覆盖率为 99.2%<sup>[1](https://micropython.org/?lang=en)</sup>。

## 相对于 C 的性能

在 ESP32 和 [Raspberry Pi Pico](https://www.edgepedia.org/zh/raspberry-pi-pico) 硬件上比较 MicroPython 与 Arduino C++ 的基准测试显示，I/O 比 C 慢 10 至 20 倍，内存占用高 4 至 6 倍，但仍足以胜任常见的传感器和网络任务<sup>[6](https://arxiv.org/abs/2608.18160)</sup>。在 240 MHz 的 ESP32-S3 上，通过 MicroPython 的 `pin.value()` 翻转 GPIO 引脚耗时 22.80 微秒，而 C++ 的 `digitalWrite()` 为 1.45 微秒，约慢 15.7 倍，因为 Python 调用必须先解析字节码、解析对象并调用 C API，然后才能写入寄存器<sup>[15](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)</sup>。在一百万次迭代的循环中，整数加法在 MicroPython 中耗时 48.5 毫秒，而优化后的 C++ 为 1.8 毫秒；`math.sin(x)` 耗时 415 毫秒，而后者为 34 毫秒<sup>[15](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)</sup>。

**内存与时间成本。** 在同一开发板上，MicroPython 固件占用约 1.6 MB 闪存，解释器在用户脚本运行前为 Python 堆、垃圾回收器和模块注册表预留约 240 KB SRAM<sup>[15](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)</sup>。3 至 8 毫秒的垃圾回收暂停可能干扰驱动 WS2812B 可寻址 LED 等严格时序协议，因此时序关键代码应在紧凑循环前调用 `gc.collect()`，并使用硬件卸载驱动，而非用纯 Python 软件模拟引脚信号时序<sup>[15](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)</sup>。MicroPython 限制中断服务例程内代码的操作，不允许内存分配<sup>[15](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)</sup>。

这些成本换来的是另一种工作方式：脚本可通过 USB 大容量存储或 OTA 更新，无需固件编译周期，REPL 支持在硬件上即时实验<sup>[15](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)</sup><sup> • </sup><sup>[5](https://learn.adafruit.com/micropython-basics-what-is-micropython/overview?view=all)</sup>。启动很快：在 168 MHz 运行的 PYBv1.1 上，从启动到加载第一个脚本仅需 150 微秒<sup>[1](https://micropython.org/?lang=en)</sup>。

## References

1. [MicroPython – Python for microcontrollers (official site)](https://micropython.org/?lang=en)
2. [The Evolution of MicroPython (10 Years Poster), micropython.org](https://micropython.org/resources/MicroPython10YearsPoster.pdf)
3. [Interview With Damien George, Creator Of The Micro Python Project, Hackaday (27 November 2013)](https://hackaday.com/2013/11/27/interview-with-damien-george-creator-of-the-micro-python-project/)
4. [micropython/micropython, GitHub repository README](https://github.com/micropython/micropython)
5. [MicroPython Basics: What is MicroPython?, Adafruit Learning System](https://learn.adafruit.com/micropython-basics-what-is-micropython/overview?view=all)
6. [MicroPython and CircuitPython: Pythons Quiet Takeover of IoT and Robotics, arXiv:2608.18160](https://arxiv.org/abs/2608.18160)
7. [CircuitPython vs MicroPython: What's the Difference, pythonresources.com](https://pythonresources.com/guides/circuitpython-vs-micropython/)
8. [MicroPython downloads](https://micropython.org/download/)
9. [The Compiler, MicroPython developer documentation](https://micropython.org/resources/docs/en/latest/develop/compiler.html)
10. [MicroPython on microcontrollers (constrained devices), MicroPython documentation](https://docs.micropython.org/en/latest/reference/constrained.html)
11. [MicroPython ports, micropython.com](https://micropython.com/language/ports/)
12. [MicroPython Support Tiers, MicroPython documentation](https://micropython.org/resources/docs/en/latest/develop/support_tiers.html)
13. [Circuit Python, Adafruit Industries](https://www.adafruit.com/circuitpython)
14. [Adafruit CircuitPython API Reference – Differences from MicroPython](https://docs.circuitpython.org/en/stable/docs/)
15. [Arduino MicroPython Performance Benchmarks: C++ vs Python Speed, ElectricalFlux](https://electricalflux.com/mcu-boards/arduino-micropython-cpp-speed-benchmark-tests)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Interpreters and language runtimes

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/micropython

English original: https://www.edgepedia.org/micropython
