py5 in Thonny – installation and the first sketch
In this course we draw and animate in py5: that is Processing for real Python 3. We work in Thonny, the same environment we use for the Python basics and for MicroPython on the micro:bit. That way a student works in one program all year.
Python Mode in Processing 4 is no longer maintained and uses the old Python 2.7 (Jython). Drawing and animation work, but the libraries for the serial connection and sound often do not – exactly the ones needed for the micro:bit. Details: Processing 4 – problems and choosing a path (SR).
Step 1 – Thonny
Download Thonny from thonny.org and install it. If you already have it from the basic course, update it to the latest version. py5 needs Python 3.11 or newer, and Thonny brings it along.
Step 2 – the thonny-py5mode plug-in
- In Thonny open Tools → Manage plug-ins…
- Search for
thonny-py5modeand click Install. - Close and restart Thonny. A py5 item appears in the menu.
Step 3 – turn on imported mode
In the py5 menu tick Imported mode for py5. The first time, the plug-in downloads and sets up Java (a JDK) by itself – this takes a few minutes, only once. You do not have to install Java by hand or set JAVA_HOME.
when you write ordinary Python programs from the basic course (input, print), turn off imported mode. It is meant only for programs that draw.
Step 4 – the first sketch
# py5 in Thonny (imported mode): no import statement and no py5. in front of the functions
def setup():
size(600, 400)
def draw():
background(240)
fill(0, 150, 255)
no_stroke()
circle(mouse_x, mouse_y, 50) # the circle follows the mouse
Run it with the green ▶ button. A window opens with a circle that follows the mouse.
What changes compared with Processing Python Mode
The logic of the program stays the same: setup(), draw(), coordinates, colours. The names change: instead of a capital letter in the middle (mouseX), you write a lower-case letter and an underscore (mouse_x).
| Processing Python Mode | py5 (imported mode) |
|---|---|
mouseX, mouseY | mouse_x, mouse_y |
pmouseX | pmouse_x |
mousePressed (variable) | is_mouse_pressed |
def mousePressed(): | def mouse_pressed(): |
keyPressed (variable) | is_key_pressed |
def keyPressed(): | def key_pressed(): |
keyCode | key_code |
noStroke(), noFill() | no_stroke(), no_fill() |
strokeWeight(3) | stroke_weight(3) |
textSize(20), textAlign(CENTER) | text_size(20), text_align(CENTER) |
rectMode(CENTER), ellipseMode(…) | rect_mode(CENTER), ellipse_mode(…) |
frameCount | frame_count |
frameRate(30) | frame_rate(30) |
noLoop() | no_loop() |
map(v, a, b, c, d) | remap(v, a, b, c, d) |
loadImage("image.png") | load_image("image.png") |
PVector | Py5Vector |
add_library('serial') + serialEvent() | import serial (pyserial), reading in draw() |
size, background, fill, stroke, ellipse, rect, line, circle, text, width, height, random and constrain are written the same way, so many examples work without changes.in imported mode do not give your variables the names of py5 functions (e.g.
size, text, width). Write circle_size, message, paddle_width.py5 and the micro:bit
To read data from the micro:bit, install the pyserial package: Tools → Manage packages…, search for pyserial, Install. An example of a whole project is in the lesson the micro:bit as a game controller (SR), and the basics are in the lesson micro:bit → py5 over the serial port.
the same Thonny is used for the micro:bit. When you have flashed the program onto the micro:bit, switch the interpreter to Local Python 3 (the lower right corner of Thonny), and only then run the py5 sketch that reads the port.
Check your knowledge
Open the quiz
Mini quiz: py5 in Thonny
1. How is mouseX written in py5?
2. What does the thonny-py5mode plug-in do the first time you turn on imported mode?
3. When do you turn off imported mode?
4. What is the function map() called in py5?
Useful external resources
- py5 – the official documentation – guides and a function reference
- thonny-py5mode – the plug-in for Thonny
- The status of Python Mode – the official explanation by the Processing team
- Thonny
