MicroPython modules import order

Python looks for modules in sys.path.

Python has a simple algorithm for finding a module with a given name, such as acme. It looks for a file called acme.py in the directories listed in the variable sys.path.

The search order for modules in MicroPython is a bit different and is as follows :

  1. Built-in modules: The interpreter checks for the presence of the module in the list of built-in modules. Built-in modules are written in C and are part of the MicroPython interpreter. They are imported automatically and are available to the user without the need to import them explicitly.
  2. Frozen modules: If the module is not a built-in module, the interpreter checks for the presence of the module in the .frozen folder. Frozen modules are Python modules that are included in the MicroPython firmware image and are available to the interpreter when it runs.
  3. Modules on the filesystem: If the module is not found in the .frozen folder, the interpreter searches for the module on the filesystem of the device. The search starts at the root of the filesystem and proceeds through the directory tree until the module is found.

special entry .frozen :

On MicroPython, an entry with the value ".frozen" will indicate that import should search frozen modules at that point in the search. If no frozen module is found then search will not look for a directory called .frozen, instead it will continue with the next entry in sys.path.

Import priority

When using import in micropython it will look for the file according to the priority defined by the position in sys.path. So it will look at / and /lib first, before looking in the frozen firmware modules. However if the frozen modules of the firmware include a main.py it will autostart this and ignore any main.py located in /.

Example :

import sys

sys.path
['', '.frozen', '/lib']

Misc notes