Can't get 3-back psychopy experiment to run on Pavlovia

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.2.3
What are you trying to achieve?:
I am trying to run a 3-back experiment online

What did you try to make it work?:
Initially I typed all code components in python and used the Auto → JS function. Upon learning that this function does not work properly on Pavlovia (which can not handle many import functions), I started trying to manually convert my python code into the appropriate javascript code. I have gone through various forums to look for potential solutions as new errors kept cropping up with each change. I have run into the perpetual initializing experiment issue (often with the debug error Uncaught SyntaxError: unexpected …) numerous times and when the experiment has run, I inevitably get some version of the “ReferenceError: (variable) is not defined error” immediately after my first routine (which is just an Instruction screen). I have also been referencing the Python to JS crib sheet and trying to implement as many suggestions listed there as I can.

I am by no means a competent coder and have been trying to teach myself as I go using this online service. The experiment almost always works fine offline in Psychopy but I am kind of at a loss at this point trying to use Pavlovia. My experiment is public and labeled “3-back_nonrandom” under the user cn442 so if anyone can help me further troubleshoot my code, I would greatly appreciate it (not sure how to locate the link directly to my code so I apologize for asking you to search for it in the Explore tab). The error message I am currently getting is the perpetual initializing experiment issue with the following debug error:


Hi There,

Firstly thanks for making your project public and sharing the name of it - I was able to find it easily still so am taking a look at your files now :slight_smile: Can I please check - in the project there are 2 .psyexp files “3-back” and “3-back_nonrandom” - which would you like me to look at?


Thank you for the quick reply and sorry for the confusion. I would like you to look at “3-back_nonrandom”

No problem at all. OK I can see that because the non random version has all the code components in JS the downloaded copy doesn’t run offline, which makes it a bit tricky to debug (but the 3-back runs just fine). Can I ask if you have a local copy of the version with the original python ->JS code components available to share?


PS. the auto>JS tool usually works fine for Pavlovia, it is just specific bits of code that haven’t yet been built into the translate tool that need manually converting (e.g. see pop() vs. shift() in the crib sheet you shared)

3-back_nonrandom.psyexp (39.0 KB) 3-back_loop_nonrandom.csv (1.5 KB)

You’re not able to run this program offline in Psychopy? That’s odd since I’m not having any trouble running it myself despite changing the code to JS for Pavlovia. Might these files (which I have uploaded directly from my computer as opposed to extracting from github) possibly work better? If not, the “3-back” experiment is essentially the same as the “3-back_nonrandom” with some minor changes to the second subloop where I select rows from the xlsx/csv file (the nonrandom version selects 10 sequential rows, such as 1-10 or 31-40, whereas the original version just selects rows at random). The code in the “3-back” experiment is the original code in python whereas I converted the python to JS as best I could in the “3-back_nonrandom”

I’m sorry I am not making this easy for you but I do appreciate the help. The python code I was originally using seemed to require that I use the import function which is where the initial problems with Pavlovia started. It was from there that I had to start converting to JS and each change seemed to bring up a new error.

Don’t worry at all. Getting around these JS translations is the main thing in Pavlovia at the moment.

First, in terms of why the downloaded file didn’t work for me (not your main question but an interesting side note):
So because things like your stimuli are set in code in this exp (e.g. your letters list), and because the PsychoPy App runs in python, if your code components are entirely in JS, the desktop (python) App doesn’t use these (because they are specified in JS). I think this is why the downloaded copy won’t run offline for me. It is curious that this should run in desktop for you though (but we can come back to that as it is not your main question! just an interesting side note)

For your current question:
I have had a play with your 3-back and I think I see the problem. You use a python library (random) in order to use the function choice. The JS side doesn’t have the “random” library available. So we need to import the equivalent. So you may need:
Random = Math.random;
At the start of your script in a JS component (this one can be a JS only component, because the python app will never need it). This is under the “maths functions” sections of the crib sheet you shared.

For a bit more context, can I ask why you use code components to select letter stimuli rather than a conditions file? is it because you want the selected letters to be in an entirely random order? An example n-back with a conditions file (that uses a preset “sequential” order) is available on pavlovia here :blush: if you haven’t had a play with that one it might also be useful.

Let me know how you get on,

Could you point me more specifically to where I am using the “random” library and function choice? I can’t seem to find it myself as I thought I removed all import functions for python libraries and the like. I see where I’ve inserted the Math.random function you mentioned but that would be doing it correctly, no? Are you referring to the looptype choice of random in the loop “back3targetloop”? I will add the line of code you suggested to my code_JS custom component where I’ve included the other suggestions from that crib sheet. Were you able to get it to work (either offline with the files I uploaded or even online in Pavlovia)?

I’ve implemented the additional line of code regarding the Random component but I still get the perpetual initializing experiment issue. This is the debug message:


As far as why I used the letter stimuli inside the code component, that was honestly the first idea I had to implement the experiment. Yes, I am trying to randomly select the letter stimuli.

Sure. So random is imported in the file “3-back.psyexp” the routine “routine_3_back_target” the component “code” and the “Begin Routine” tab :slight_smile:

Aha I didn’t realise you had already imported Math.random already though - looks like you were on the right debugging tracks - could you point me to where that is (if in the same file I am working off)

The “3-back.psyexp” worked fine for me offline. the nonrandom didn’t.


Should I just restart with the “3-back” experiment and try to apply these minimal changes while keeping Auto->JS selected? I can only type code into the left side when the Auto->JS option is selected and whenever I would try to substitute JS code there, I would get this green text on the right side that seemed to indicate I was incorrectly typing JS code where python code is supposed to go. This is what gave me the impression that I couldn’t just replace the problematic python lines with JS lines and leave the rest alone (having both JS and python lines of code in the left window) and therefore had to try and type the whole code component script in JS with the corresponding option selected.

I would recommend starting from the 3-back if the second script is minimal changes from that. I would also highly recommend checking out the existing repository to see if that can be adapted for what you need :slight_smile:

to type code into both sides select “both” under code type in your code component :slight_smile:


Thank you for all your help Becca. Hopefully I can resolve this issue quickly and won’t be back again to bother you more :slight_smile:

no worries - good luck!!