模块¶
Generated Fri 19 Jun 2026 22:08:45 UTC
仅限位置参数¶
为节省代码体积,CPython 中许多接受关键字参数的函数在 MicroPython 中只接受位置参数。
MicroPython 与 CPython 采用相同的方式标记仅限位置参数,即插入 / 来标记位置参数的结束。任何签名以 / 结尾的函数仅接受位置参数。详情请参见 PEP 570。
示例¶
例如,在 CPython 3.4 中,构造函数 socket.socket 的签名如下::
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
然而,MicroPython 中记录的签名为::
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
参数末尾的 / 表示在 MicroPython 中这些参数均为仅限位置参数。以下代码在 CPython 中可运行,但在大多数 MicroPython 移植版本中不可运行::
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
MicroPython 将引发异常::
TypeError: function doesn't take keyword arguments
以下代码在 CPython 和 MicroPython 中均可运行::
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array¶
不支持不同类型码之间的比较¶
原因: 代码体积限制。
解决方法: 逐元素进行比较。
示例代码:
import array
array.array("b", [1, 2]) == array.array("i", [1, 2])
CPython output: | MicroPython output: |
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
NotImplementedError:
|
未实现溢出检查¶
原因: MicroPython 实现了隐式截断以减少代码体积和执行时间。
解决方法: 若需与 CPython 兼容,请显式对值进行掩码处理。
示例代码:
import array
a = array.array("b", [257])
print(a)
CPython output: | MicroPython output: |
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
OverflowError: signed char is greater than maximum
| array('b', [1])
|
未实现整数查找¶
示例代码:
import array
print(1 in array.array("B", b"12"))
CPython output: | MicroPython output: |
False
| Traceback (most recent call last):
File "<stdin>", line 10, in <module>
NotImplementedError:
|
未实现数组删除¶
示例代码:
import array
a = array.array("b", (1, 2, 3))
del a[1]
print(a)
CPython output: | MicroPython output: |
array('b', [1, 3])
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
TypeError: 'array' object doesn't support item deletion
|
步长不为 1 的下标访问尚未实现¶
示例代码:
import array
a = array.array("b", (1, 2, 3))
print(a[3:2:2])
CPython output: | MicroPython output: |
array('b')
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
NotImplementedError: only slices with step=1 (aka None) are supported
|
errno¶
MicroPython does not include ENOTSUP as a name for errno 95.¶
Cause: MicroPython does not implement the ENOTSUP canonical alias for EOPNOTSUPP added in CPython 3.2.
Workaround: Use errno.EOPNOTSUPP in place of errno.ENOTSUP.
示例代码:
import errno
print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")
CPython output: | MicroPython output: |
errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
| errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP
|
json¶
JSON 模块在对象不可序列化时不抛出异常¶
示例代码:
import json
try:
print(json.dumps(b"shouldn't be able to serialise bytes"))
except TypeError:
print("TypeError")
CPython output: | MicroPython output: |
TypeError
| "shouldn't be able to serialise bytes"
|
os¶
environ 属性未实现¶
解决方法: 使用 getenv、putenv 和 unsetenv。
示例代码:
import os
try:
print(os.environ.get("NEW_VARIABLE"))
os.environ["NEW_VARIABLE"] = "VALUE"
print(os.environ["NEW_VARIABLE"])
except AttributeError:
print("should not get here")
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))
CPython output: | MicroPython output: |
None
VALUE
| should not get here
None
VALUE
|
getenv 返回实际值而非缓存值¶
原因: environ 属性未实现。
示例代码:
import os
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))
CPython output: | MicroPython output: |
None
None
| None
VALUE
|
random¶
getrandbits 方法每次最多只能返回 32 位。¶
原因: PRNG 的内部状态仅为 32 位,因此每次最多只能返回 32 位数据。
解决方法: 若需要超过 32 位的数值,请使用 micropython-lib 中的 random 模块。
示例代码:
import random
x = random.getrandbits(64)
print("{}".format(x))
CPython output: | MicroPython output: |
10880755926996596610
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less
|
randint 方法只能返回不超过原生字长大小的整数。¶
原因: PRNG 每次只能生成 32 位状态,结果被转换为原生大小的 int 而非完整的 int 对象。
解决方法: 若需要大于原生字长的整数,请使用 micropython-lib 中的 random 模块。
示例代码:
import random
x = random.randint(2**128 - 1, 2**128)
print("x={}".format(x))
CPython output: | MicroPython output: |
x=340282366920938463463374607431768211456
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
OverflowError: overflow converting long int to machine word
|
struct¶
Struct pack with too few args, not checked by MicroPython¶
示例代码:
import struct
try:
print(struct.pack("bb", 1))
print("Should not get here")
except:
print("struct.error")
CPython output: | MicroPython output: |
struct.error
| b'\x01\x00'
Should not get here
|
Struct pack with too many args, not checked by MicroPython¶
示例代码:
import struct
try:
print(struct.pack("bb", 1, 2, 3))
print("Should not get here")
except:
print("struct.error")
CPython output: | MicroPython output: |
struct.error
| b'\x01\x02'
Should not get here
|
Struct pack with whitespace in format, whitespace ignored by CPython, error on MicroPython¶
原因: MicroPython 针对代码体积进行了优化。
解决方法: 不要在格式字符串中使用空格。
示例代码:
import struct
try:
print(struct.pack("b b", 1, 2))
print("Should have worked")
except:
print("struct.error")
CPython output: | MicroPython output: |
b'\x01\x02'
Should have worked
| struct.error
|
sys¶
无法覆盖 sys.stdin、sys.stdout 和 sys.stderr¶
原因: 它们存储在只读内存中。
示例代码:
import sys
sys.stdin = None
print(sys.stdin)
CPython output: | MicroPython output: |
None
| Traceback (most recent call last):
File "<stdin>", line 10, in <module>
AttributeError: 'module' object has no attribute 'stdin'
|