Skip to content

Enable multi-file projects - #115

Merged
laurensvalk merged 1 commit into
masterfrom
multi-file-projects
Aug 26, 2022
Merged

laurensvalk merged 1 commit into
masterfrom
multi-file-projects

Conversation

@laurensvalk

@laurensvalk laurensvalk commented Aug 24, 2022 •

Copy link
Copy Markdown
Member

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 .py files in Pybricks Code. Equivalently, create multiple .py files in one folder if you use Pybricksdev.

Then the modules can import (from) each other as you would with any normal Python modules:

from my_module import A
print(A)

import my_other_module
print(my_other_module.something)

Consistent with Python, the following will work for the main program being run (= has focus in Pybricks Code):

if __name__ == "__main__":
    print("Let's test this module individually")

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:

  • Efficiently loading .mpy files using upcoming MicroPython features.
  • Pybricks Code with a basic file browser

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:

   - first script size (uint32 little endian)
   - first script name (no extension, zero terminated string)
   - first script mpy data
   - second script size
   - second script name
   - second script mpy data
   - ...

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 ImportError will 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.

Comment thread bricks/stm32/main.c Resolved
Comment thread bricks/stm32/main.c Resolved
Comment thread bricks/stm32/main.c Resolved
Comment thread bricks/stm32/main.c Resolved
Comment thread bricks/stm32/main.c Resolved
Comment thread bricks/stm32/main.c Resolved
Comment thread bricks/stm32/main.c Resolved
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
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

Copy link
Copy Markdown
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.

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.

2 participants