Precedence of .mpy & .py

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
poesel
Posts: 12
Joined: Sun Oct 20, 2019 4:58 pm

Precedence of .mpy & .py

Post by poesel » Mon Feb 17, 2020 8:49 pm

Hi,

if I have:

/lib/foo.py
/lib/foo.mpy
&
sys.path=['/lib']

Which module has precedence?

What if:

/lib/foo.py
/libmpy/foo.mpy
&
sys.path=['/libmpy','/lib']

What happens here?

Thanks!

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

Re: Precedence of .mpy & .py

Post by jimmo » Tue Feb 18, 2020 1:49 am

Given the statement: "import foo"

The implementation is:

Code: Select all

for each dir in sys.path:
  try foo/  (i.e "foo" is a package)
  try foo.py
  try foo.mpy
I'm fairly sure the answer is therefore "foo.py" in the first case, and "/libmpy/foo.mpy" in the second case. But this is pretty easy to confirm for yourself -- see viewtopic.php?f=2&t=7350&p=42007

poesel
Posts: 12
Joined: Sun Oct 20, 2019 4:58 pm

Re: Precedence of .mpy & .py

Post by poesel » Wed Feb 19, 2020 7:12 pm

Thanks - your explanation and the linked thread helped me understand it!

Post Reply