getting started
Compile Python source into a self-contained Scratch 3 project (.sb3). The generated project runs Python with standard Scratch blocks, variables and lists. It needs no browser extension, Python server or network connection at runtime.
Quick start
From the repository root:
python -m pip install .
py2scratch examples/language_demo.py language_demo.sb3
Or use the compiler directly without installation:
python py2scratch.py examples/language_demo.py language_demo.sb3
Open the Scratch editor, select File → Load from your computer, choose the generated project, and click the green flag. The output appears in the project’s output list.
The compiler packages its runtime and bundled module implementations into each
project. The optional --native mode emits direct blocks for the original
numeric subset.
Compile a project
From the repository root, use Python 3.10 or newer.
python py2scratch.py examples/language_demo.py language_demo.sb3
On Windows, py can be used in place of python. The optional --native flag retains direct block output for the original numeric subset. The default compiler uses the full Scratch block runtime.
Install and use as a library
python -m pip install .
py2scratch example.py example.sb3
from pyscratch_compiler import compile_file, compile_source
compile_file("examples/language_demo.py", "language_demo.sb3")
compile_source('print(sum(range(10)))', "sum.sb3")
Pass a source_path to compile_source when an in-memory source string imports files relative to an entry script. The compiler reads source and bundles modules without executing their top-level code on your computer. The code runs when the green flag starts the resulting Scratch project.