mpy-cross questions

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
jedie
Posts: 92
Joined: Fri Jan 29, 2016 12:32 pm

mpy-cross questions

Post by jedie » Wed Dec 04, 2019 6:33 pm

Question 1:
I i have foo.py and foo.mpy on device? Which one does MicroPython use when importing?

Question 2:
Will mpy-cross strip all (block-)DocStrings from .mpy files? So it costs no RAM?

jedie
Posts: 92
Joined: Fri Jan 29, 2016 12:32 pm

Re: mpy-cross questions

Post by jedie » Fri Dec 06, 2019 1:09 pm

I notice that the optimizations level, set with `-O`, changes the information in tracebacks. At higher levels, for example, the code line number is always 0.

Is there more detailed information about this somewhere?

I started https://gitlab.com/alelec/mpy_cross/issues/5 for this, too.

jedie
Posts: 92
Joined: Fri Jan 29, 2016 12:32 pm

Re: mpy-cross questions

Post by jedie » Sat Dec 07, 2019 8:54 am

jedie wrote:
Wed Dec 04, 2019 6:33 pm
I i have foo.py and foo.mpy on device? Which one does MicroPython use when importing?
Next question: I have "foo" and frozen module and as .py and .mpy ... Which one does MicroPython use when importing?

User avatar
pythoncoder
Posts: 3716
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mpy-cross questions

Post by pythoncoder » Sat Dec 07, 2019 9:48 am

A quick experiment indicates that foo.py is imported in preference.
Peter Hinch

stijn
Posts: 372
Joined: Thu Apr 24, 2014 9:13 am

Re: mpy-cross questions

Post by stijn » Sat Dec 07, 2019 10:22 am

Yes, if I'm not mistaken the sequence is frozen module > directory > .py > .mpy

jedie
Posts: 92
Joined: Fri Jan 29, 2016 12:32 pm

Re: mpy-cross questions

Post by jedie » Sat Dec 07, 2019 10:32 am

stijn wrote:
Sat Dec 07, 2019 10:22 am
Yes, if I'm not mistaken the sequence is frozen module > directory > .py > .mpy
Interesting. Should be somewhere inserted into the official docs, isn't it?

So it's possible to bundle the complete project in frozen modules and overwrite it with .mpy files and at least via .py files. Great!

User avatar
jimmo
Posts: 890
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: mpy-cross questions

Post by jimmo » Sat Dec 07, 2019 11:41 am

I replied to the bug explaining the optimisation level.

I agree that the whole .mpy / frozen / import ordering area is a bit poorly documented and could use some work. I saw in PR #5083 that Damien is currently working on docs for the native module support, so maybe we can extend on that. I actually started writing something myself earlier this week but got distracted by #5377 instead... But what pythoncoder and stjin said is correct with respect the ordering -- for all the gory details look at py/builtinimport.c

FWIW, most of the same people reading the bugs also read the forums so no need to ask the same question in all the places :)

User avatar
Roberthh
Posts: 1741
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: mpy-cross questions

Post by Roberthh » Sat Dec 07, 2019 2:40 pm

The order of searching files from the interned frozen modules or the file system is defined by sys.path.
The empty string in sys.path represents both the frozen files and the working directory. If you change sys.path to list places from your file system first, and have the empty string at the end, then frozen files are the last one to look for. Example:
The default value of sys.path is : ['', '/lib']
Changing that to ['.', '/lib', ''] causes the working directory and /lib to be searched first, and then again the frozen modules and the working directory.

User avatar
pythoncoder
Posts: 3716
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mpy-cross questions

Post by pythoncoder » Sun Dec 08, 2019 7:35 am

@jedie As a general point, while we are always glad to help, there are two routes to answering questions like this for yourself. One is to study the source: admittedly hard work, but very educational.

As a bone-idle empiricist I cheated with an experiment. I created a file foo.py containing only

Code: Select all

print('foo')
and cross-compiled it to foo.mpy. I then edited foo.py to change the message. Issuing

Code: Select all

import foo
settled the issue.
Peter Hinch

Post Reply