How to randomly select video stimuli (coding) and import them with the Builder

Hi everyone!
I’m Marta, and I’m brand new to Psychopy and Python.
I just created an experiment with Psychopy in which I’ve to present a series of videos.
I have two conditions, let’s call them Cond1 and Cond2, and each condition has 60 videos.
I want to select randomly only 20 videos for each condition and then shuffle the chosen videos and present them to the participant.
I created the structure of the experiment with the builder, and then I wrote down a few lines of code to randomize and select the videos.

The problem is: how to let the builder and the code communicate?
The code works on Jupyter, but it does not find the videos when I run it in Psychopy.

In the image, you can see the top part where there is the “Words_RealExp” routine, I have the parallel port, the videos, and the lines of code.

The “trials_2” that I highlighted in blue selects a .csv file with the names of all the videos that I have in my folder.

I’d really appreciate it if someone could help me with finding a solution.


Hello Marta,

if your code-component contains the relevant code to shuffle and to select the videos make it the top-component of your three components (right-click, move to top).

Best wishes Jens

Thanks, Jens, for your suggestion, I implemented it, but it still does not work.
I suppose that something is incorrect in my code.

The code is this:

Import packages

import random
import pandas as pd
import numpy as np

pandas read csv file

Lista=pd.DataFrame.from_csv(‘List_V2_Exp_copia.csv’, sep = ‘,’, index_col = None)

create lists with only 20 out of 60 elements random

list1 = Lista[Lista[‘Cond’]==10][‘Path1’].sample(20)
list2 = Lista[Lista[‘Cond’]==20][‘Path1’].sample(20)

concatenate the 2 lists (alias the 2 conditions)

mylist = list(list1) + list(list2)

#randomize elements across lists ( = conditions)

—> The list I read as csv at the beginning is the same list that I load in “trials_2”. This file contains two columns, the first one with the names of the videos that I have, and the second one has “10” and “20” that I added just to divide the videos into the two conditions that I need.

Do you think that I need to update the code with something else?

Thanks in advance for your help! I really appreciated