模組

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 屬性未實作

因應方式: 改用 getenvputenvunsetenv

範例程式碼::

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 個位元。

原因: 偽隨機數產生器的內部狀態僅 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 方法回傳的整數最大不超過原生字組大小。

原因: 偽隨機數產生器每次只能產生 32 位元的狀態,結果被轉型為原生大小的整數而非完整的 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'