UnboundLocalError: local variable 'trialList' referenced before assignment

OS (e.g. Win10): mac 10.15.4
PsychoPy version (e.g. 1.84.x): 2020.1.2
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:

I have this old exp that I can still run [github link removed after problem solved ]. I just refactored it: the stimuli are slightly different, but I meant to use the same paradigm [github link removed after problem solved ]. I copied the .psyexp file over, and when I run it, psychopy crushes after instruction presentation and gives me

2020-05-18 23:32:20.511 python[2618:50957] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Traceback (most recent call last):
  File "/Users/xzfang/Desktop/exp1/popout_cindy_lastrun.py", line 267, in <module>
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/data/utils.py", line 417, in importConditions
    (fileName, len(trialList), len(fieldNames)))
UnboundLocalError: local variable 'trialList' referenced before assignment
##### Experiment ended. #####

I am stumped, waiting for help.

This happens for a reason: probably I did something different, but the error message doesn’t make any sense.

So I pip installed psychopy2020.1.2 into my conda python3 env and ran the script from the terminal. Crash, then correct error messages are shown: I fixed it and the script is running now (from both terminal and runner). The runner is not showing correct error messages, a psychopy bug?

Traceback (most recent call last):
  File "popout_cindy_lastrun.py", line 246, in <module>
  File "/Users/xzfang/anaconda3/lib/python3.7/site-packages/psychopy/data/utils.py", line 285, in importConditions
    trialList, fieldNames = pandasToDictList(trialsArr)
  File "/Users/xzfang/anaconda3/lib/python3.7/site-packages/psychopy/data/utils.py", line 248, in pandasToDictList
    _assertValidVarNames(fieldNames, fileName)
  File "/Users/xzfang/anaconda3/lib/python3.7/site-packages/psychopy/data/utils.py", line 223, in _assertValidVarNames
    (fileName, msg, os.linesep * 2, name))
ValueError: Conditions file tBlock01.csv: Parameters (column headers) cannot contain punctuation or spaces

0.5678 	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', 'pyo', 'pygame'] (in that order).
1.9216 	WARNING 	Import Error: No module named 'psychtoolbox'. Using event module for keyboard component.
4.3426 	WARNING 	User requested fullscreen with size [1536  864], but screen is actually [1440, 900]. Using actual size


I’m currently receiving the same error message about ‘trialList’ being referenced before assignment. What did you do to fix it? Any help would be much appreciated.


I had the same error and found out that it was caused by an oversight in the csv file,
which was generated by another script:

There were leading spaces on the column names.
After I removed them, things worked.

Hope that helps

Traceback (most recent call last):
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\builder\dialogs\__init__.py", line 1605, in onBrowseTrialsFile
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\data\utils.py", line 447, in importConditions
    (fileName, len(trialList), len(fieldNames)))
UnboundLocalError: local variable 'trialList' referenced before assignment

CSV file has no spaces or other marking.
I haven’t used any other script so far to have interference with this.
I would appreciate any help.
Thank you in advance!

Hi @ar2 I found a partial solution to this. Copying the cell data into a new csv file and overwriting the existing file solved this error for me.

Though it’s still not clear why this problem is occurring.

I got the same error. I checked the .cvs: two columns had the same name.
I changed them, made sure that all column names differed and the problem got solved.
Hope this could help!

1 Like

I was getting the same error when I was trying to add my .csv conditions file to a loop. Referring to some of the above answers, I ensured that there were no spaces in my column names. It turns out that Psychopy didn’t like that one of my column names had a slash (I was trying to define a variable as old/new). But I generally use underscore a lot in my column names to make it easier during data cleaning and analysis and that hasn’t given me any errors in Psychopy so far.
Hope this helps others who are getting a similar error!