MicroPython 库¶
警告
本章节的重要概述
本章描述了 MicroPython 中内置的模块(函数和类库)。 本文档通常旨在描述在 MicroPython 项目中实现的所有模块和函数/类。 但是,MicroPython 是高度可配置的,特定板/嵌入式系统的每个适配端可能仅包含可用 MicroPython 库的子集。
基于这一点,请注意本文档中描述的模块(甚至整个模块)中的某些函数/类在特定系统上的特定 MicroPython 构建中 也许不可用。 了解特定功能的可用性/不可用性的最佳位置是包含特定 MicroPython 适配端 有关信息的 “信息概要” 内容。
在一些适配端你可以通过在 REPL 输入以下命令查看可用的内置库:
help('modules')
除了本文档中描述的内置库之外,还可以在 micropython-lib 中找到 Python 标准库中的更多模块,以及它对 MicroPython 的进一步扩展。
Python 标准库和微型库¶
以下标准 Python 库已被 “微型化” 以符合 MicroPython 的理念。 它们提供了其模块的核心功能,旨在成为标准 Python 库的替代品。
array– 数值数据数组binascii– 二进制/ASCII 转换builtins– builtin functions and exceptionscmath– mathematical functions for complex numberscollections– collection and container typeserrno– system error codesgc– control the garbage collectorhashlib– hashing algorithmsheapq– heap queue algorithmio– input/output streamsjson– JSON encoding and decodingmath– mathematical functionsos– basic “operating system” servicesrandom– generate random numbersre– simple regular expressionsselect– wait for events on a set of streamssocket– socket modulessl– SSL/TLS modulestruct– pack and unpack primitive data typessys– system specific functionstime– time related functionsuasyncio— asynchronous I/O schedulerzlib– zlib decompression_thread– multithreading support
MicroPython 的特定库¶
以下库中提供了特定于 MicroPython 实现的功能。
bluetooth— low-level Bluetoothbtree– simple BTree databasecryptolib– cryptographic ciphersframebuf— frame buffer manipulationmachine— functions related to the hardwaremicropython– access and control MicroPython internalsneopixel— control of WS2812 / NeoPixel LEDsnetwork— network configurationuctypes– access binary data in a structured way
适配端的特定库¶
在某些情况下,以下适配端/板特定库具有类似于 machine 库中的功能或类。 在这种情况下,适配端特定库中的对应项会声明该平台独有的硬件功能。
为了编写可移植代码,请使用 machine 模块中的函数和类。 要访问特定于平台的硬件时,请使用适当的库,例如在 Pyboard 上使用 pyb 。
pyboard 的特定库¶
下述的库是 pyboard 特有的。
WiPy 的特定库¶
下述的库和类是 WiPy 特有的。
ESP8266 和 ESP32 的特定库¶
下述的库是 ESP8266 和 ESP32 特有的。
RP2040 的特定库¶
下述的库和类是 RP2040 特有的,用于树莓派 Pico 板。
Zephyr 的特定库¶
下述的库和类是 Zephyr 适配端特有的。
扩展 Python 内置库¶
在大多数情况下,上述模块实际上命名为 umodule 而不是 module,但 MicroPython 会将任何以 u 为前缀的模块别名为非 u 版本。 但是,名为 module.py 的文件(或 冻结的 module )将优先于此别名。
该特性允许用户提供内置库的扩展实现(可能提供额外的 CPython 兼容性)。 用户提供的模块(指 module.py 中)仍可通过直接导入 umodule 来使用内置功能。 这种做法在 micropython-lib 中广泛使用。 有关详细信息,请参阅 分发包、包管理和部署应用程序 。
这在 Python 标准库(例如 os 、time 等)和 MicroPython 库(例如 machine 、 bluetooth 等)同样适用。 主要的例外是特定于适配端的库( pyb 、 esp 等)。
除非你特别想强制使用内置模块,我们推荐始终使用 ``import module`` 而不是 ``import umodule`` 。