mpy-cross questions
mpy-cross questions
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?
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
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.
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
Next question: I have "foo" and frozen module and as .py and .mpy ... Which one does MicroPython use when importing?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: mpy-cross questions
A quick experiment indicates that foo.py is imported in preference.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: mpy-cross questions
Yes, if I'm not mistaken the sequence is frozen module > directory > .py > .mpy
Re: mpy-cross questions
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
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
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
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: mpy-cross questions
@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
and cross-compiled it to foo.mpy. I then edited foo.py to change the message. Issuing
settled the issue.
As a bone-idle empiricist I cheated with an experiment. I created a file foo.py containing only
Code: Select all
print('foo')
Code: Select all
import foo
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: mpy-cross questions
On ESP8266 the default sys.path is: ['', '/lib', '/']
Sadly i can't change the path in e.g. boot.py:
Seems that's hardcoded in ports/esp8266/qstrdefsport.h:
But how is the first entry '' inserted?
EDIT: Ah! sys.path.insert(0, '.') works in boot.py so i will use this...
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'
Code: Select all
...
// qstrs specific to this port, only needed if they aren't auto-generated
// Entries for sys.path
Q(/)
Q(/lib)
EDIT: Ah! sys.path.insert(0, '.') works in boot.py so i will use this...