mpy-cross

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

mpy-cross

Post by KJM » Fri Jan 28, 2022 5:01 am

I finally got a version of mpy-cross running in Windows 10

Code: Select all

C:\Users\Admin>pip install mpy-cross
Collecting mpy-cross
  Downloading mpy_cross-1.18-py2.py3-none-win_amd64.whl (158 kB)
     |████████████████████████████████| 158 kB 6.8 MB/s
Installing collected packages: mpy-cross
Successfully installed mpy-cross-1.18
unfortunately the upython on my esp32 is version 1.17.
1) Does anybody know what pip install cmd would get me the 1.17 version of mpy-cross?
2) Can someone give me a link that explains how to use mpy-cross to produce a .mpy file

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: mpy-cross

Post by mattyt » Fri Jan 28, 2022 7:15 am

Take a look at mpy-cross v1.17 on PyPi.

Code: Select all

pip install mpy-cross==1.17
You'll also find basic documentation on how to use it on that page. Basic use:

Code: Select all

mpy-cross yourfile.py

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: mpy-cross

Post by KJM » Fri Jan 28, 2022 11:12 pm

That don't work for me Matt

Code: Select all

C:\Users\Admin>mpy-cross test.py
OSError: 2
If I get into the options at the bottom of that page I get

Code: Select all

C:\Users\Admin>python -m mpy_cross -v -X main.py
usage: C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mpy_cross\mpy-cross.exe [<opts>] [-X <implopt>] <input filename>
Options:
--version : show version information
-o : output file for compiled bytecode (defaults to input with .mpy extension)
-s : source filename to embed in the compiled bytecode (defaults to input file)
-v : verbose (trace various operations); can be multiple
-O[N] : apply bytecode optimizations of level N

Target specific options:
-msmall-int-bits=number : set the maximum bits used to encode a small-int
-mno-unicode : don't support unicode in compiled strings
-march=<arch> : set architecture for native emitter; x86, x64, armv6, armv7m, armv7em, armv7emsp, armv7emdp, xtensa, xtensawin

Implementation specific options:
  emit={bytecode,native,viper} -- set the default code emitter
  heapsize=<n> -- set the heap size for the GC (default 2097152)
I feel like it's trying to tell me something but I'm not sure what it is.

Also, any tips on how to install on a Rpi? Maybe it would run there if I could just figure out how to install it?

Code: Select all

root@raspberrypi:/home/pi# pip install mpy-cross==1.17
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting mpy-cross==1.17
  Could not find a version that satisfies the requirement mpy-cross==1.17 (from versions: )
No matching distribution found for mpy-cross==1.17

root@raspberrypi:/home/pi# pip3 install mpy-cross==1.17
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting mpy-cross==1.17
  Could not find a version that satisfies the requirement mpy-cross==1.17 (from versions: )
No matching distribution found for mpy-cross==1.17

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: mpy-cross

Post by KJM » Sat Jan 29, 2022 7:11 am

OK I finally got mpy-cross 1.17 to work from the windows cmd line. I fed it a 29k .py file & it spat out a 10k .mpy file. My problem now is it doesn't seem to work. I've tried loading it onto the esp32 as main.mpy or main.py, neither seems to run at power up. I've tried importing it in the repl but it doesn't run after I do.

This is proving much harder than I expected. Any thoughts on why the .mpy is only half the size of the original .py & what I might do to find out what's wrong with it?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: mpy-cross

Post by scruss » Sat Jan 29, 2022 3:37 pm

50% size reduction sounds about right. But what are you expecting mpy-cross to do?

It's mostly used for reducing the file size and memory footprint of micropython modules. I haven't seen it used to compile main.py, but lots of libraries in /lib benefit from being run through mpy-cross

Post Reply