Module¶
Generated Fri 19 Jun 2026 22:08:45 UTC
Nur-Positions-Parameter¶
Um Codegröße zu sparen, akzeptieren viele Funktionen, die in CPython Schlüsselwortargumente annehmen, in MicroPython nur Positionsargumente.
MicroPython kennzeichnet Nur-Positions-Parameter auf dieselbe Weise wie CPython, indem ein / eingefügt wird, um das Ende der Positionsparameter zu markieren. Jede Funktion, deren Signatur auf / endet, nimmt ausschließlich Positionsargumente entgegen. Weitere Informationen finden Sie unter PEP 570.
Beispiel¶
In CPython 3.4 lautet beispielsweise die Signatur des Konstruktors socket.socket:
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Die in MicroPython dokumentierte Signatur lautet jedoch:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
Das / am Ende der Parameter zeigt an, dass sie in MicroPython alle nur als Positionsargumente übergeben werden können. Der folgende Code funktioniert in CPython, aber nicht in den meisten MicroPython-Ports:
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
MicroPython löst eine Ausnahme aus:
TypeError: function doesn't take keyword arguments
Der folgende Code funktioniert sowohl in CPython als auch in MicroPython:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array¶
Vergleich zwischen verschiedenen Typecodes nicht unterstützt¶
Ursache: Codegröße
Abhilfe: Vergleichen Sie einzelne Elemente
Beispielcode:
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:
|
Überlaufprüfung ist nicht implementiert¶
Ursache: MicroPython implementiert implizites Abschneiden, um Codegröße und Ausführungszeit zu reduzieren
Abhilfe: Wenn CPython-Kompatibilität erforderlich ist, maskieren Sie den Wert explizit
Beispielcode:
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])
|
Suche nach Ganzzahl nicht implementiert¶
Beispielcode:
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:
|
Array-Löschung nicht implementiert¶
Beispielcode:
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
|
Subskript mit Schritt != 1 ist noch nicht implementiert¶
Beispielcode:
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.
Beispielcode:
import errno
print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")
CPython output: | MicroPython output: |
errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
| errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP
|
json¶
Das JSON-Modul löst keine Ausnahme aus, wenn ein Objekt nicht serialisierbar ist¶
Beispielcode:
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¶
Das Attribut environ ist nicht implementiert¶
Abhilfe: Verwenden Sie getenv, putenv und unsetenv
Beispielcode:
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 gibt den tatsächlichen Wert statt des zwischengespeicherten Werts zurück¶
Ursache: Das Attribut environ ist nicht implementiert
Beispielcode:
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¶
Die Methode getrandbits kann jeweils maximal 32 Bit zurückgeben.¶
Ursache: Der interne Zustand des PRNG beträgt nur 32 Bit, sodass er jeweils maximal 32 Bit Daten zurückgeben kann.
Abhilfe: Wenn Sie eine Zahl mit mehr als 32 Bit benötigen, verwenden Sie das random-Modul aus micropython-lib.
Beispielcode:
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
|
Die Methode randint kann nur eine Ganzzahl zurückgeben, die höchstens die native Wortgröße hat.¶
Ursache: Der PRNG kann jeweils nur 32 Bit Zustand erzeugen. Das Ergebnis wird dann in einen nativ großen int statt in ein vollständiges int-Objekt umgewandelt.
Abhilfe: Wenn Sie Ganzzahlen benötigen, die größer als die native Wortgröße sind, verwenden Sie das random-Modul aus micropython-lib.
Beispielcode:
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¶
Beispielcode:
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¶
Beispielcode:
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¶
Ursache: MicroPython ist auf Codegröße optimiert.
Abhilfe: Verwenden Sie keine Leerzeichen in Formatstrings.
Beispielcode:
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¶
Überschreiben von sys.stdin, sys.stdout und sys.stderr nicht möglich¶
Ursache: Sie sind im Nur-Lese-Speicher abgelegt.
Beispielcode:
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'
|