Cross compiling to .mpy

I ran the following code on OpenMV Cam H7, FW Ver 4.3.3 and got an AttributeError: ‘tuple’ object has no attribute ‘_mpy’

import sys
sys_mpy = sys.implementation._mpy
arch = [None, 'x86', 'x64',
    'armv6', 'armv6m', 'armv7m', 'armv7em', 'armv7emsp', 'armv7emdp',
    'xtensa', 'xtensawin'][sys_mpy >> 10]
print('mpy version:', sys_mpy & 0xff)
print('mpy sub-version:', sys_mpy >> 8 & 3)
print('mpy flags:', end='')
if arch:
    print(' -march=' + arch, end='')

That’s not a feature that’s documented? sys – system specific functions — MicroPython 1.20 documentation

I followed the instructions here:

I am trying to cross compile .py to .mpy. I performed the following steps but am hungup at the ./mpy-cross step, getting error “./mpy-cross: No such file or directory”

  1. Installed Ubuntu on Windows 11
  2. Clone Micropython Git Repository: $ git clone GitHub - micropython/micropython: MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems
  3. Installed Make: $ sudo apt install make
  4. Installed Bash: $ sudo apt install bash
  5. Updated the Ubuntu package: $ sudo apt update
  6. Installed the build-essential package: $ sudo apt install build-essential
  7. Checked the GCC version: $ gcc –version
    gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
  8. Checked Bash Version: $ bash --version
    GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
  9. Checked Python 3 installed: $ python3
    Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
  10. Make mpy-cross: ~/micropython/mpy-cross $ make
  11. Run mpy-cross: ~/micropython/mpy-cross$ ./mpy-cross -h
    ERROR -bash: ./mpy-cross: No such file or directory

What am I missing?

Make mpy-cross: ~/micropython/mpy-cross $ make

This should have run and generated a binary like this:

CC ../shared/runtime/gchelper_generic.c
LINK build/mpy-cross
   text    data     bss     dec     hex filename
 326528   13784     872  341184   534c0 build/mpy-cross

Is this the case?


LINK build/mpy-cross
text data bss dec hex filename
325862 13784 872 340518 53226 build/mpy-cross

:~/micropython/mpy-cross $ ls
Makefile build main.c mphalport.h mpy_cross gccollect.c mpconfigport.h mpy-cross.vcxproj qstrdefsport.h

Can you follow this guide here first: openmv/src at master · openmv/openmv (

  1. Run mpy-cross: ~/micropython/mpy-cross$ ./mpy-cross -h
    ERROR -bash: ./mpy-cross: No such file or directory

Typically means the file was not executable.

I found mpy-cross in the build subfolder

~/micropython/mpy-cross/build$ ls
gccollect.P gccollect.o genhdr main.P main.o mpy-cross py shared

It works from there.
~/micropython/mpy-cross/build$ ./mpy-cross -h
~/micropython/mpy-cross/build$ ./mpy-cross ~/micropython/examples/

cross compiled .mpy is not compatable. ValueError: incompatible .mpy file

From the openmv docs “Check the validity of the .mpy file by inspecting the first two bytes of the file. The first byte should be an uppercase ‘M’ and the second byte will be the version number, which should match the system version from above.”

First byte of file is ‘M’, second byte is 0x06, which matches the mpy-cross version.
:~/micropython/mpy-cross/build$ ./mpy-cross --version
MicroPython v1.23.0-preview.174.gbc424ddc4 on 2024-03-01; mpy-cross emitting mpy v6.2

Now back to my original question in this thread … How do I determine the .mpy version and flags supported by the MicroPython my system by executing?

Is this the micropython version? v1.18?

MicroPython: v1.18-omv-r13 OpenMV: v4.3.3 HAL: v1.9.0 BOARD: OPENMV4-STM32H743

From the table in the openmv docs, mycropython v1.18 is .mpy version 5.

How do I change the .mpy version to match? Where do I find the Git commit for .mpy version 5?

Uh… we’re at 4.5.2 so… you’re quite far behind the tip of the repo. The current release is at 1.20.