Skip to content

Importing modules, take 2 - #33

Closed
laurensvalk wants to merge 2 commits into
masterfrom
multifile
Closed

laurensvalk wants to merge 2 commits into
masterfrom
multifile

Conversation

@laurensvalk

@laurensvalk laurensvalk commented Jun 9, 2022 •

Copy link
Copy Markdown
Member

This is not quite ready to be merged, but it is too much fun to not share. It provides an interesting exploration for what we can do now that Pybricks Code supports multiple files.

This PR lets us do multi-file projects of the following kind:

pybricksdev

It relies on MicroPython's existing mpy-tool to merge .mpy files and run them all one by one. Because of this, no changes to the firmware are required.

To make it work, it looks for import statement of the form:

from module1 import xyz

# Or

from module1 import *

In both cases, everything gets "imported", as if substituted in-place. Except without altering the line numbers. Not perfect, but still very useful.

This has similarities with #20, but it is simpler to implement.

I have not implemented any recursive imports in this demo yet. So right now, you can only use imports from the main script that you are running. I also limited it to the import variants shown above. But this is fine for the flat file structure we have in Pybricks Code right now (no folders/projects, just one directory with files.).

This takes the files as-is from MicroPython 1.18. Maybe this could be
turned into a published package one day.
@laurensvalk

Copy link
Copy Markdown
Member Author

Closing in favor of real imports in #37

@laurensvalk laurensvalk closed this Aug 9, 2022
@laurensvalk
laurensvalk deleted the multifile branch August 26, 2022 19:06
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