Page 1 of 2

mpy-cross questions

Posted: Wed Dec 04, 2019 6:33 pm
by jedie
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?

Re: mpy-cross questions

Posted: Fri Dec 06, 2019 1:09 pm
by jedie
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.

Re: mpy-cross questions

Posted: Sat Dec 07, 2019 8:54 am
by jedie
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?

Re: mpy-cross questions

Posted: Sat Dec 07, 2019 9:48 am
by pythoncoder
A quick experiment indicates that foo.py is imported in preference.

Re: mpy-cross questions

Posted: Sat Dec 07, 2019 10:22 am
by stijn
Yes, if I'm not mistaken the sequence is frozen module > directory > .py > .mpy

Re: mpy-cross questions

Posted: Sat Dec 07, 2019 10:32 am
by jedie
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!

Re: mpy-cross questions

Posted: Sat Dec 07, 2019 11:41 am
by jimmo
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 :)

Re: mpy-cross questions

Posted: Sat Dec 07, 2019 2:40 pm
by Roberthh
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.

Re: mpy-cross questions

Posted: Sun Dec 08, 2019 7:35 am
by pythoncoder
@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.

Re: mpy-cross questions

Posted: Sun Jan 05, 2020 6:44 pm
by jedie
On ESP8266 the default sys.path is: ['', '/lib', '/']

Sadly i can't change the path in e.g. boot.py:

Code: Select all

>>> sys.path=['/', '/lib', '']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'path'
Seems that's hardcoded in ports/esp8266/qstrdefsport.h:

Code: Select all

...
// qstrs specific to this port, only needed if they aren't auto-generated

// Entries for sys.path
Q(/)
Q(/lib)
But how is the first entry '' inserted?

EDIT: Ah! sys.path.insert(0, '.') works in boot.py so i will use this...