Compilation croisée pour Micro-Python

Objectif :
Permettre de pré-compiler le code source d’un programme en  Python en bytecode spécifique à un matériel.
Le Raspberry Pico est donc notre matériel cible.
Les fichiers “.py” seront générés en “.mpy“.

Distribution de travail : Lubuntu 22.04

Pour en savoir plus :
https://pypi.org/project/mpy-cross/
https://docs.micropython.org/en/latest/reference/mpyfiles.html
https://www.ardusimple.com/distribute-mpy-precompiled-files/

1/ Installation de l’outil de gestion de paquet Python : pip

util01@station40:~$ sudo apt install python3-pip

2/ Installation de l’environnement de compilation-croisé.

util01@station40:~$ pip install mpy-cross
Defaulting to user installation because normal site-packages is not writeable
Collecting mpy-cross
Downloading mpy_cross-1.19.1-py2.py3-none-manylinux1_x86_64.whl (122 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 122.7/122.7 KB 489.1 kB/s eta 0:00:00
Installing collected packages: mpy-cross
WARNING: The script mpy-cross is installed in '/home/util01/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed mpy-cross-1.19.1

3/ Vérifier si l’outil est installé.

util01@station40:~$ /home/util01/.local/bin/mpy-cross --version
MicroPython v1.19.1 on 2022-06-18; mpy-cross emitting mpy v6

4/ Exemple.

– Créer le fichier “bonjour.py” avec le contenu suivant :

print("Bonjour tout le monde")

– Exécuter le code afin de vérifier si tout fonctionne correctement :

util01@station40:~/HACKLABFR/mpy$ python3 bonjour.py
Bonjour tout le monde

– Cross-compiler ensuite le programme :

util01@station40:~/HACKLABFR/mpy$ python3 -m mpy_cross bonjour.py

– Vérification :

util01@station40:~/HACKLABFR/mpy$ ls -l bonjour.*
-rw-r--r-- 1 util01 util01 58 nov. 29 18:30 bonjour.mpy
-rw-r--r-- 1 util01 util01 31 nov. 29 18:29 bonjour.py

– Copier le fichier ‘bonjour.mpy‘ dans le Raspberry Pico avec l’IDE Thonny par exemple.

– Pour exécuter le programme, dans la fenêtre ‘Shell‘, importer le fichier ‘.mpy’ en tant que module :

import bonjour

Les commentaires sont fermés