Repository navigation
Enable multi-file projects - #115
Merged
Merged
Conversation
This was referenced Aug 24, 2022
dlech
reviewed
Aug 24, 2022
laurensvalk
added a commit
that referenced
this pull request
Aug 26, 2022
This allows the main program to import other mpy modules that are appended to it. It remains possible to run single-scripts for backwards compatibility with existing IDE tools. See #115
laurensvalk
force-pushed
the
multi-file-projects
branch
from
August 26, 2022 18:59
8f5e281 to
e4d4dc7
Compare
This allows the main program to import other mpy modules that are appended to it. It remains possible to run single-scripts for backwards compatibility with existing IDE tools. See #115
laurensvalk
force-pushed
the
multi-file-projects
branch
from
August 26, 2022 20:02
e4d4dc7 to
34888da
Compare
Member
Author
|
I'm merging this so we have a working point to start from, but the functionality and implementation/format are subject change and open to user feedback. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This enables a script to import one or more other modules that are downloaded to the hub along with it.
This is useful to organize your code and to share common modules with others.
How will this work?
You create multiple
.pyfiles in Pybricks Code. Equivalently, create multiple.pyfiles in one folder if you use Pybricksdev.Then the modules can import (from) each other as you would with any normal Python modules:
Consistent with Python, the following will work for the main program being run (= has focus in Pybricks Code):
In the other modules,
__name__is the module name as expected.This makes it possible to develop each module separately and still effectively use them in other scripts.
Background
This is a frequently requested feature (e.g. pybricks/support#189, pybricks/support#289, pybricks/support#203)
This was made possible by two recent major breakthroughs:
Pybricksdev / Pybricks Code updates
Some changes to Pybricks Code would be needed to support this, but this PR ensures that existing versions of Pybricks Code and Pybricksdev will still work, to ensure a smooth transition.
As before, there is one total size and one payload (previously one mpy blob) sent via Bluetooth.
The payload is changed to this format:
To find out which scripts must be included Pybricksdev / Pybricks Code recursively finds the modules being imported by the current script.
If a script is not found it can be silently ignored by the editor, since an appropriate
ImportErrorwill be raised when running the program, just as with any Python program.Currently, builtin modules take precedence, but since we use a custom import handler we could turn this around if we want to allow users to "override" (mask) builtin names.
Try it out
Upgrade Pybricksdev to v1.0.0-alpha.30.
Install the latest master branch firmware
Using Pybricksdev, download and run a program using BLE as usual. It should now be able to import python files residing in the same folder.