PsychoPy – a basic example

PsychoPy – a basic example

PsychoPy is an excellent tool for preparing psychological experiments in Python.

However, it seems that entry-level examples one can find in the web are rather sophisticated. Hence, I prepared this basic example featuring:

  1. Installing PsychoPy on Linux
  2. Displaying a simple stimulus
  3. Simplest possible keyboard event

Installing PsychoPy on Linux

Open a terminal (5 Ways to Open a Terminal Console Window Using Ubuntu).

Install PsychoPy:

sudo pip install psychopy

I don’t recommend beginners using PsychoPy on Windows. Especially on some older machines. Windows is generally not Python friendly, and installing all dependencies and virtual environments can be harder for a beginner even than installing a dual-boot Linux or just getting some old PC and installing Lubuntu there.

Display simple stimuli

Open some text editor, e.g., Gedit (on Ubuntu), paste and save the following text to a filename named, e.g.: “”:

# Import modules
from psychopy import visual, core

# Create a window
window = visual.Window([400,400])

# Prepare a fixation point (we will draw it in the next step)
fixation = visual.TextStim(window, text='+')

# Draw the stimulus

# Refresh/update the window (with the fixation point drawn on)

# Show the fixation point for 2 seconds

# Prepare the first stimulus
message = visual.TextStim(window, text='Stimulus!')

# Draw the stimulus

# Show the window after drawing the stimulus

# Display the stimulus for 2 seconds

Then use the terminal to run the file with a command:


Example based on:

Keyboard example

Now we will run a simple keyboard example script. Copy-paste the following text and save it as “”:

# Import modules
from psychopy import visual, core, event

# Create a simple window
window = visual.Window([400,400])

# Show the window (it should be empty, but we need it to record keyboard events)

# An infinite loop
while True:
    # Get keys pressed by the participant
    keys = event.getKeys()
    # If the participant strokes "q"
    if 'q' in keys:
        # just quit the program
    for key in keys:
        # Else print the key to the terminal

Run the example in the terminal with the following command:


This example is also available as a gist:

Leave a Reply