Skip to content

Commit

Permalink
pybricksdev/compile.py: Add better error message
Browse files Browse the repository at this point in the history
When compiling multi-file projects that contain a namespace pacakge
(folder without __init__.py), we hit Python bug python/cpython#84530.

This adds a try/except block to catch the error and raise a more helpful
error message.

Issue: pybricks/support#1602
  • Loading branch information
dlech committed Jun 30, 2024
1 parent 978c346 commit a1a8a56
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Use relative paths when compiling multi-file projects.
- Better error message when hitting Python bug when compiling multi-file projects.

### Fixed
- Fixed `pybricksdev` BLE commands not working on Windows when `pythoncom`
Expand Down
8 changes: 7 additions & 1 deletion pybricksdev/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ async def compile_multi_file(path: str, abi: Union[int, Tuple[int, int]]):
# compile files using Python to find imports contained within the same directory as path
search_path = [os.path.dirname(path)]
finder = ModuleFinder(search_path)
finder.run_script(path)

try:
finder.run_script(path)
except AttributeError as e:
raise RuntimeError(
"ModuleFinder doesn't currently handle implicit namespace packages. Did you forget to put an __init__.py file in one of your subdirectories? See https://github.com/pybricks/support/issues/1602"
) from e

# we expect missing modules, namely builtin MicroPython packages like pybricks.*
logger.debug("missing modules: %r", finder.any_missing())
Expand Down

0 comments on commit a1a8a56

Please sign in to comment.