@@ -509,7 +509,7 @@ async def stop_user_program(self) -> None:
509509
510510 async def run (
511511 self ,
512- py_path : str ,
512+ py_path : Optional [ str ] = None ,
513513 wait : bool = True ,
514514 print_output : bool = True ,
515515 line_handler : bool = True ,
@@ -518,7 +518,8 @@ async def run(
518518 Compiles and runs a user program.
519519
520520 Args:
521- py_path: The path to the .py file to compile.
521+ py_path: The path to the .py file to compile. If None, runs a
522+ previously downloaded program.
522523 wait: If true, wait for the user program to stop before returning.
523524 print_output: If true, echo stdout of the hub to ``sys.stdout``.
524525 line_handler: If true enable hub stdout line handler features.
@@ -532,11 +533,17 @@ async def run(
532533 self ._stdout_buf .clear ()
533534 self ._stdout_line_queue = asyncio .Queue ()
534535 self .print_output = print_output
535- self .script_dir , _ = os .path .split (py_path )
536536 self ._enable_line_handler = line_handler
537+ self .script_dir = os .getcwd ()
538+ if py_path is not None :
539+ self .script_dir , _ = os .path .split (py_path )
537540
538541 # maintain compatibility with older firmware (Pybricks profile < 1.2.0).
539542 if self ._mpy_abi_version :
543+ if py_path is None :
544+ raise RuntimeError (
545+ "Hub does not support running stored program. Provide a py_path to run"
546+ )
540547 await self ._legacy_run (py_path , wait )
541548 return
542549
@@ -546,9 +553,10 @@ async def run(
546553 "Hub is not compatible with any of the supported file formats"
547554 )
548555
549- mpy = await compile_multi_file (py_path , 6 )
556+ if py_path is not None :
557+ mpy = await compile_multi_file (py_path , 6 )
558+ await self .download_user_program (mpy )
550559
551- await self .download_user_program (mpy )
552560 await self .start_user_program ()
553561
554562 if wait :
0 commit comments