← Other topics

PsychoPy - Manual install via Conda (PsychoPy in VSCode)

Video Notes

I wanted to be able to code PsychoPy experiments without the standalone PsychoPy program. This would allow me to work in my preferred code editor (VSCode) instead of PsychoPy’s editor. I wanted to do this using Conda.

As of this writing, the steps PsychoPy provides for installing via Anaconda and Conda failed for me when running a simple PsychoPy script (details on this failure at the end of this guide).

As an alternative, I manually set up a Conda environment and installed PsychoPy there. The following notes summarize the steps for doing this.

Conda

To complete these steps you should have access to the conda command from your command line program.

If you don’t, follow the instructions here to download and install it: https://docs.anaconda.com/free/miniconda/index.html.

Create environment

Create a new Conda environment:

> conda create --name demo

Activate that environment:

> conda activate demo

Install Python and packages

Install Python 3.10 in the environment:

> conda install python=3.10

In the PsychoPy docs, they recommend Python 3.8 up to 3.10 with a preference for 3.8:

[...] we strongly recommend you use Python 3.8. Older Python versions are no longer being tested and may not work correctly. Newer Python versions may not have wheels for all the necessary dependencies even though we believe that PsychoPy’s code, itself, is compatible all the way up to Python 3.10.

In my tests, PsychoPy wouldn’t install without error using Python 3.8 so I opted for 3.10.

Next, we’ll, install the psychopy package via pip using the following command. Pip is a Python package manager that was installed as part of the above Python installation. We’re using pip to install psychopy instead of Conda because - as of this writing - the pip package repository has a more up to date version of psychopy (2023.2.3 vs. Conda’s 2022.2.5).

Install psychopy package via pip:

> pip install psychopy

I also suggest installing pytables:

> conda install pytables

Without pytables, I was receiving the following warning:

WARNING: pytables package not found. ioHub functionality will be disabled.

Example experiment

With my PsychoPy environment set up, I created a simple Stroop experiment to test it. Below is the code which I placed in a named file stroop.py and then ran in my environment like so:

> python stroop.py
from psychopy import core, visual, event
import random

# Define colors
colors = ["red", "green", "blue"]

# Create the window
win = visual.Window(size=(800, 600), color="white")

# Create the instructions
instructions = visual.TextStim(win, color="black", text="Press the key corresponding to the color of the word you see on the screen.\n\n R = Red\n G = Green\n B = Blue\n \nPress the space bar to start.")

# Create the text stimuli
text = visual.TextStim(win=win, color="white", height=0.5)

# Initialize variables
n_trials = 3 # Number of trials
correct_responses = 0

# Define data collection
data = [] 

# Display instructions
instructions.draw()
win.flip()
event.waitKeys()

# Run experiment loop
for _ in range(n_trials):

    # Randomly choose a color and word
    color = colors[random.randint(0, len(colors) - 1)]
    word = colors[random.randint(0, len(colors) - 1)]
    
    # Set text color and word
    text.text = word
    text.color = color

    # Present the stimuli
    text.draw()
    win.flip()

    # Start timer
    start_time = core.getTime()

    # Collect response
    response = event.waitKeys(keyList=['r', 'g', 'b'])
    if response:
        key_pressed = response[0]
    
    # Calculate reaction time
    reaction_time = core.getTime() - start_time

    # Check response accuracy
    if color[0] == key_pressed:
        correct_responses += 1

    # Store trial data
    data.append({
        "color": color, 
        "word": word, 
        "reaction_time": reaction_time, 
        "correct": color[0] == key_pressed
    })

# Show results
print("Number of trials:", n_trials)
print("Number of correct responses:", correct_responses)
print("Accuracy:", correct_responses / n_trials)
print("Data:", data)

# Close window
win.close()
core.quit()

How the "Anaconda and Miniconda" install failed

When following PsychoPy’s instructions for installing via Anaconda and Miniconda I ran into the following road blocks. I’m including these details here just as documentation of the issue and to help others who might find this guide via search engines.

Step one is setting up the env file the documentation provides:

name: psychopy
channels:
- conda-forge
dependencies:
- python=3.8
- psychopy
- pip
- pip:
  - psychtoolbox
  - pygame
  - pyo
  - pyparallel; platform_system != "Windows"
  - SoundFile; platform_system == "Windows"
  - websocket_client

When I attempted to create an environment with this env file as written (conda env create -n psychopy -f psychopy-env.yml), I received the following error:

LibMambaUnsatisfiableError: Encountered problems while solving:
  - package psychopy-2022.1.4-py38h10201cd_0 requires pyobjc-framework-quartz, but none of the providers can be installed

Could not solve for environment specs
The following packages are incompatible
├─ psychopy is installable with the potential options
│  ├─ psychopy [2022.1.4|2022.2.5] would require
│  │  └─ pyobjc-framework-quartz with the potential options
│  │     ├─ pyobjc-framework-quartz [10.0|10.1|...|9.2] would require
│  │     │  └─ python_abi 3.10.* *_cp310, which can be installed;
│  │     ├─ pyobjc-framework-quartz [10.0|10.1|...|9.2] would require
│  │     │  └─ python_abi 3.9.* *_cp39, which can be installed;
│  │     ├─ pyobjc-framework-quartz [10.0|10.1|...|9.2] would require
│  │     │  └─ python_abi 3.11.* *_cp311, which can be installed;
│  │     └─ pyobjc-framework-quartz [10.0|10.1|9.2] would require
│  │        └─ python_abi 3.12.* *_cp312, which can be installed;
│  ├─ psychopy [2022.1.4|2022.2.5] would require
│  │  └─ python_abi 3.9.* *_cp39, which can be installed;
│  └─ psychopy 2022.2.5 would require
│     └─ python_abi 3.10.* *_cp310, which can be installed;
└─ python 3.8**  is not installable because there are no viable options
   ├─ python [3.8.10|3.8.12|...|3.8.8] would require
   │  └─ python_abi 3.8.* *_cp38, which conflicts with any installable versions previously reported;
   └─ python [3.8.11|3.8.13|...|3.8.18] conflicts with any installable versions previously reported.

This error indicates the package manager is unable to find a compatible set of versions for all the required packages, leading to an unsatisfiable configuration.

To address this, I tried bumping up the Python requirement from v3.8 to 3.10 in the env file. This allowed the environment set up to complete successfully, but when I attempted to run the above stroop.py script, it failed with numerous errors that looked like the following (full error output here):

objc[34942]: Class wxNSAppController is implemented in both /Users/Susan/miniconda3/envs/psychopy/lib/libwx_osx_cocoau_core-3.2.0.dylib (0x104fd1a78) and /Users/Susan/miniconda3/envs/psychopy/lib/libwx_osx_cocoau_core-3.2.0.2.1.dylib (0x1104f1a78). One of the two will be used. Which one is undefined.

/Users/runner/miniforge3/conda-bld/wxpython_1707147130012/work/ext/wxWidgets/src/common/object.cpp(239): assert "classTable->Get(m_className) == __null" failed in Register(): Class "wxCommandEvent" already in RTTI table - have you used wxIMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?

At this point I abandoned my attempt to set up PsychoPy using their provided env file and followed the steps described at the start of this guide.

If this info helped you out, you can send a tip via PayPal. Thanks for your support!
← Other topics