MicroPython 库



  • MicroPython 为每个模块实现了 Python 功能的子集。

  • 为易于扩展,标准 Python 模块的 MicroPython 版本通常具有 u (micro) 前缀。

  • 任何特定 MicroPython 变体 (或 Port) 都可能缺少在此一般文档编制中所描述的任何特征/功能 (由于资源约束或其它局限性)。

本章节描述内置于 MicroPython 中的模块 (函数和类库)。这些模块他为几种类别:

  • 实现标准 Python 功能子集,且不打算由用户扩展的模块。

  • 实现 Python 功能子集,并由用户提供扩展 (通过 Python 代码) 的模块。

  • 实现对 Python 标准库的 MicroPython 扩展的模块。

  • 特定模块,尤其 MicroPython port 且因此不可移植。

Note about the availability of the modules and their contents: This documentation in general aspires to describe all modules and functions/classes which are implemented in MicroPython project. However, MicroPython is highly configurable, and each port to a particular board/embedded system makes available only a subset of MicroPython libraries. For officially supported ports, there is an effort to either filter out non-applicable items, or mark individual descriptions with “Availability:” clauses describing which ports provide a given feature.

With that in mind, please still be warned that some functions/classes in a module (or even the entire module) described in this documentation may be unavailable in a particular build of MicroPython on a particular system. The best place to find general information of the availability/non-availability of a particular feature is the “General Information” section which contains information pertaining to a specific MicroPython port .

On some ports you are able to discover the available, built-in libraries that can be imported by entering the following at the REPL:


Beyond the built-in libraries described in this documentation, many more modules from the Python standard library, as well as further MicroPython extensions to it, can be found in micropython-lib .

Python 标准库和 Micro 库

The following standard Python libraries have been “micro-ified” to fit in with the philosophy of MicroPython. They provide the core functionality of that module and are intended to be a drop-in replacement for the standard Python library. Some modules below use a standard Python name, but prefixed with “u”, e.g. ujson 而不是 json . This is to signify that such a module is micro-library, i.e. implements only a subset of CPython module functionality. By naming them differently, a user has a choice to write a Python-level module to extend functionality for better compatibility with CPython (indeed, this is what done by the micropython-lib project mentioned above).

On some embedded platforms, where it may be cumbersome to add Python-level wrapper modules to achieve naming compatibility with CPython, micro-modules are available both by their u-name, and also by their non-u-name. The non-u-name can be overridden by a file of that name in your library path ( sys.path ). For example, import json will first search for a file json.py (or package directory json ) and load that module if it is found. If nothing is found, it will fallback to loading the built-in ujson 模块。

Port 特定库

In some cases the following port/board-specific libraries have functions or classes similar to those in the machine library. Where this occurs, the entry in the port specific library exposes hardware functionality unique to that platform.

To write portable code use functions and classes from the machine module. To access platform-specific hardware use the appropriate library, e.g. pyb in the case of the Pyboard.