Variable not defined pavlovia

URL of experiment: garsinis [PsychoPy]

Description of the problem: Hello, I synced my experiment with pavlovia, but when I try to run it, it raises an error that a variable randDuration is not defined, though I checked the code and everything seemed to be ok, the variable is simple and is defined properly in every routine (the variable is randDuration = random.randint(1000, 2000)/1000). The code works offline properly too

I would guess that the problem isn’t with assigning a value to the variable, but when you try to use the variable in something else. It can be helpful to pinpoint where in your code the error occurs exactly. See this tutorial for some pointers on how to find that out: Thomas Pronk / tutorial_js_semantic_error · GitLab

I couldn’t find a solution using the tutorial, so I copied the code from psychopy builder which caused the problem:

------Prepare to start Routine “trp”-------

continueRoutine = True

update component parameters for each repeat

import random
randDuration = random.randint(1000, 2000)/1000

keep track of which components have finished

trpComponents = [polygon_39]
for thisComponent in trpComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, ‘status’):
thisComponent.status = NOT_STARTED

reset timers

t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock=“now”)
trpClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1

-------Run Routine “trp”-------

while continueRoutine:
# get current time
t = trpClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=trpClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame

# *polygon_39* updates
if polygon_39.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
    # keep track of start time/frame for later
    polygon_39.frameNStart = frameN  # exact frame index
    polygon_39.tStart = t  # local t and not account for scr refresh
    polygon_39.tStartRefresh = tThisFlipGlobal  # on global time
    win.timeOnFlip(polygon_39, 'tStartRefresh')  # time at next scr refresh
    polygon_39.setAutoDraw(True)
if polygon_39.status == STARTED:
    # is it time to stop? (based on global clock, using actual start)
    if tThisFlipGlobal > polygon_39.tStartRefresh + randDuration-frameTolerance:
        # keep track of stop time/frame for later
        polygon_39.tStop = t  # not accounting for scr refresh
        polygon_39.frameNStop = frameN  # exact frame index
        win.timeOnFlip(polygon_39, 'tStopRefresh')  # time at next scr refresh
        polygon_39.setAutoDraw(False)

# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
    core.quit()

# check if all components have finished
if not continueRoutine:  # a component has requested a forced-end of Routine
    break
continueRoutine = False  # will revert to True if at least one component still running
for thisComponent in trpComponents:
    if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
        continueRoutine = True
        break  # at least one component has not yet finished

# refresh the screen
if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
    win.flip()

-------Ending Routine “trp”-------

for thisComponent in trpComponents:
if hasattr(thisComponent, “setAutoDraw”):
thisComponent.setAutoDraw(False)

the Routine “trp” was not non-slip safe, so reset the non-slip timer

routineTimer.reset()

set up handler to look after randomisation of conditions etc

trials_6 = data.TrialHandler(nReps=1, method=‘random’,
extraInfo=expInfo, originPath=-1,
trialList=data.importConditions(‘5_kv_1.xlsx’, selection=‘3’),
seed=None, name=‘trials_6’)
thisExp.addLoop(trials_6) # add the loop to the experiment
thisTrial_6 = trials_6.trialList[0] # so we can initialise stimuli with some values

abbreviate parameter names if possible (e.g. rgb = thisTrial_6.rgb)

if thisTrial_6 != None:
for paramName in thisTrial_6:
exec(’{} = thisTrial_6[paramName]’.format(paramName))

for thisTrial_6 in trials_6:
currentLoop = trials_6
# abbreviate parameter names if possible (e.g. rgb = thisTrial_6.rgb)
if thisTrial_6 != None:
for paramName in thisTrial_6:
exec(’{} = thisTrial_6[paramName]’.format(paramName))

The routine consists of a fixation cross in the middle and a code component for assigning the duration of the routine. It’s the only routine where I use randDuration variable. As you can see the variable is assigned in the beginning and then used later, so I don’t really know what could be the problem :frowning:

This doesn’t work online.

You need to use randint(1000,2000)/1000 for a randome number from 1.000 to 1.999 with randint defined in code_JS as per my crib sheet.

Thank you! I’ve read the sheet and split my code component so I could write both python and js code, like this:


But when I try to sync my experiment, psychopy is not responding, it only gives back a table to mark what changes I’ve made to my project, but I can’t even type anything in there, because it freezes. I can’t compile my script to JS, it freezes too then. I’m using latest psychopy version, what could be the problem?

Hi @mykolas_gudaitis,

Pingback to your other post (about the freezing of the editor): Psychopy freezes while syncing experiment

Best, Thomas