# Cross compiling to .mpy

**URL:** https://forums.openmv.io/t/cross-compiling-to-mpy/9266
**Category:** OpenMV Boards
**Created:** [March 1, 2024, 7:58pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266 "2024-03-01T19:58:08Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 7:58pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/1 "2024-03-01T19:58:08Z")

</div>

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

```auto
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='')
print()

```

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [March 1, 2024, 9:10pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/2 "2024-03-01T21:10:41Z")

</div>

That’s not a feature that’s documented? [sys – system specific functions — MicroPython 1.20 documentation](https://docs.openmv.io/library/sys.html#module-sys)

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 9:30pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/3 "2024-03-01T21:30:52Z")

</div>

I followed the instructions here:  
[https://docs.openmv.io/reference/mpyfiles.html](https://docs.openmv.io/reference/mpyfiles.html)

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 9:38pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/4 "2024-03-01T21:38:36Z")

</div>

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](https://github.com/micropython/micropython)
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?

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [March 1, 2024, 9:48pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/5 "2024-03-01T21:48:02Z")

</div>

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

This should have run and generated a binary like this:

```auto
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?

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 10:00pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/6 "2024-03-01T22:00:24Z")

</div>

Yes

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

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 10:01pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/7 "2024-03-01T22:01:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [March 1, 2024, 10:46pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/8 "2024-03-01T22:46:26Z")

</div>

Can you follow this guide here first: [openmv/src at master · openmv/openmv (github.com)](https://github.com/openmv/openmv/tree/master/src)

> 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.

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 10:47pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/9 "2024-03-01T22:47:34Z")

</div>

I found mpy-cross in the build subfolder

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

It works from there.  
~/micropython/mpy-cross/build$ ./mpy-cross -h  
~/micropython/mpy-cross/build$ ./mpy-cross ~/micropython/examples/accel\_i2c.py

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 11:44pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/10 "2024-03-01T23:44:05Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)
#### Post date: [March 1, 2024, 11:52pm UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/11 "2024-03-01T23:52:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [March 2, 2024, 12:57am UTC](https://forums.openmv.io/t/cross-compiling-to-mpy/9266/12 "2024-03-02T00:57:26Z")

</div>

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.
