Skip to content

Conversation

@dlech
Copy link
Member

@dlech dlech commented Nov 29, 2025

We were using CPython ModuleFinder to recursively find all modules that were imported by a Python script. This mostly worked, but had some issues, like not supporting implicit namespace packages.

Let's replace it with mpy-tool from MicroPython to basically do the same thing. ModuleFinder works by compiling the file using CPython and then inspecting the bytecodes to find import statements. We can do the same without having to do the extra compiling and using mpy-tool to inspect the MicroPython bytecodes instead.

Add missing type hints in compile.py.
Change NamedTemporaryFile to TemporaryDirectory for easier cleanup via
less verbose code.
Add a new implementation of `compile_multi_file()` that uses `mpy-tool`
to find imports. This will work better than the previous implementation
that used Python's `ModuleFinder`, which had several shortcomings.

This is only implemented for MPY ABI v6 for now. To make it work for
older ABIs, we would need to get an older copy of `mpy-tool`, and
backport the json output feature to it.
@dlech
Copy link
Member Author

dlech commented Nov 29, 2025

This fixes bug 1 mentioned in pybricks/support#1602 (comment).

And also serves as a trial for a possible solution for pybricks/support#1804. (That would require re-writing mpy-tool in TypeScript - maybe a job for AI?)

@dlech dlech merged commit 31661d6 into pybricks:master Nov 29, 2025
7 checks passed
@dlech dlech deleted the mpy-tool branch November 29, 2025 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant