| Reference | Downloads | Github

Issue with Export to HTML using variable-defined condition file in loop: NameError, UnboundLocalError

URL of code: eval_pointing_1

Description of issue:
I am trying to export my experiment to HTML, and it fails every time I use a variable-defined condition file in one of my trial loops. This is used to randomize conditions.

Earlier in the experiment I define a config file by variable in a practice trial loop, and that causes no issues. Both config file name variables are declared in the same place - right at the start of the experiment.

I have verified that this is the cause of the issue by hard-coding one of the two config files in the loop. PsychoPy will then export to HTML and the experiment runs fine.

I could workaround this, but this will take a lot of effort to implement, so I am hoping there is a way to resolve this export error.

Appreciate any help!

Stdout of the Export to HTML below

pygame 1.9.6
Hello from the pygame community. Contribute - pygame wiki
6.9069 WARNING We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use [‘sounddevice’, ‘PTB’, ‘pyo’, ‘pygame’] (in that order).
7.7128 WARNING duplicate variable names: inst_panel
Alert 4051: Experiment was built in a future version of PsychoPy (2021.1.4), we recommend either updating PsychoPy or changing the “Use Version” setting in Experiment Settings to this version.
For more info see (most recent call last):
File “C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\”, line 816, in findPathsInFile
filePath = eval(filePath)
File “”, line 1, in
NameError: name ‘block_config_csv’ is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\”, line 193, in _run_module_as_main
main”, mod_spec)
File “C:\Program Files\PsychoPy3\lib\”, line 85, in _run_code
exec(code, run_globals)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 250, in
compileScript(args.infile, args.version, args.outfile)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 242, in compileScript
makeTarget(thisExp, outfile, targetOutput)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 214, in makeTarget
script = thisExp.writeScript(outfile, target=targetOutput, modular=True)
File “C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\”, line 220, in writeScript
localDateTime, modular)
File "C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\experiment\components\settings_init
.py", line 528, in writeInitCodeJS
File "C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\experiment\components\settings_init
.py", line 516, in prepareResourcesJS
resourceFiles = self.exp.getResourceFiles()
File “C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\”, line 864, in getResourceFiles
condsPaths = findPathsInFile(params[‘conditionsFile’].val)
File “C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\”, line 830, in findPathsInFile
File “C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\”, line 847, in findPathsInFile
conds = data.importConditions(thisFile[‘abs’]) # load the abs path
File “C:\Users\Vision Lab\AppData\Roaming\psychopy3\versions\psychopy\data\”, line 476, in importConditions
(fileName, len(trialList), len(fieldNames)))
UnboundLocalError: local variable ‘trialList’ referenced before assignment
65565.0625 INFO C:\Program Files\PsychoPy3\python.exe -m psychopy.scripts.psyexpCompile C:\Users\Vision Lab\Development\eval_pointing_1\eval_pointing_1.psyexp -o C:\Users\Vision Lab\Development\eval_pointing_1\html\eval_pointing_1.js -v latest


solve these problems.

  1. Make your duplicate variable names unique.
  2. Use the proper PsychoPy-version. The various PsychoPy-versions differ in features that components have.
  3. Define block_config_csv.

Best wishes Jens

Hi Jens,

Thanks for the tips.

  1. Resolved, I gave a unique name to each in its respective loop

  2. I had this set to “latest”. I tried: 2021, 2020, 2021.1.4, 2020.2, 2020.1; all of them produced the same error with block_config_csv not defined

  3. This is the crux of the issue. I had declared and assigned this variable, once at the beginning of the script, and then its value is changed right before the trial loop is run. Pictures attached.

Is there any reason why this variable is not defined?


you are showing me JS-code. I mostly rely on the auto-translate from Python → JS by PsychoPy and edit the JS only when this fails.

Anyway, could you print the value that block_config_csv at the various places in the experiment to the console and check its value and the values of its indexes? At the moment its value seems to be empty (File = “”.). What do you expect in block_config_csv = block_configs[block_num-1];?

Rather than setting the PsychoPy-version, I would update your Psycho-version to the most current one.

Best wishes Jens



I updated to PsychoPy 2021.2.1

I added console output for the values of block_config_csv = block_configs[block_num-1]; and it is correct.

console.log(block_configs) gives Array[“pointing_A3.csv”, “pointing_A4.csv”]
console.log(block_config_csv) gives pointing_A3.csv

This is true both at the beginning of the experiment,

and in the block loop (right before block_config_csv is referenced which breaks the compiler)

Still, the HTML compiler will not allow the use of block_config_csv as a variable in the Conditions field of the BlockTrials loop. It says NameError: name ‘block_config_csv’ is not defined

I can think of a workaround. Since there are only two possible block config files, I can make three block routine sequences, back to back, (A3, A4, A3) and disable one of the A3 depending on which block order is randomly chosen. But it is more laborious and will be harder to maintain in the future.

If there is anything else to try, or it could be a bug in PsychoPy, please let me know.


I remember a commentary that underscores can create problems

although with a different error message.

Best wishes Jens

Underscores should be fine unless they are the last character.

I note that line 105 of your JS code is quite far down. It isn’t in a conditional statement, is it?

I found something that works!

I named the variable “block_config” rather than “block_config_csv” and got the HTML export to work as expected.

So it must be that the “csv” in the variable name triggers a compiler function that expects something other than a string variable.

p.s. Thanks both of you for the suggestions, removing the underscores (which were within the var name, not trailing at the end) did not change the issue, and line 105 is not in a conditional statement.


cool that you found a solution.

Best wishes Jens