NameError: name ___ is not defined

I’ve ran across one more similar NameError: Within my Code Component (in Begin Routine), I’m referencing a variable from an .xlsx file, which was successfully associated with the “Trial” Loop. Specifically, I want certain stimuli presented, depending on the current condition defined in the Excel file. However, I’ve been getting another NameError, despite trying everything I could think of:

Running: /Users/Steve/Dropbox/Research and Course Files/Software/PsychoPy Stuff/Visual Search Sample/visualsearch_lastrun.py 
2016-08-07 22:24:51.589 python[12320:1121007] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/2k/x2jwcbj10_d4nvggs451003w0000gn/T/org.psychopy.PsychoPy2.savedState
Traceback (most recent call last):
  File "/Users/Steve/Dropbox/Research and Course Files/Software/PsychoPy Stuff/Visual Search Sample/visualsearch_lastrun.py", line 221, in <module>
    **if conditionLabel == "one":**
NameError: name 'conditionLabel' is not defined

Specifically, the part in bold indicates the line of code that references the Excel file. In Coder view, it appears that the Excel file is being loaded by the Trial Handler immediately prior to the start of my Trial loop, so I don’t think its an “order” issue. The experiment appears to execute if I set the variable conditionLabel in my custom code to a constant (e.g., conditionLabel = one). Still getting the hang of things, and thank you for your help!

-Steve

There are three common causes for NameError: name xxxxx is not defined:

  • Not having selected the Set every repeat option for a variable parameter to your Component (but in the particular case above that isn’t relevant because the error is coming up in a Code Component)
  • a typo in the name. Check very carefully that your capitals match those in your conditions file
  • an ordering problem (the variable being used before it has been defined).
    • If the variable is in a conditions file then it won’t be created until the relevant loop begins.
    • If your Code Component is used in multiple places, or if it has code in the Begin Experiment box then this might be executed before the variable has been created. You could add something to initialize your variable with some default value in the Begin Experiment box to be sure
5 Likes

Thanks for your help, and I’ll know to create a new topic next time.

I made a quick sample experiment, and tested a few of the built-in demos, and I think I might actually have a corrupted installation of PsychoPy, since the included demos (e.g., stroopExtended) also give me a NameError when trying to execute. I’ll reinstall and keep you updated, and thanks for the suggestions!

Steve

Again, if you want help about an error then paste it in. Always.

The error I was having was general–any time an Excel file was referenced in a loop (e.g., for the variable “letterColor” for each trial in the Stroop Extended demo) I would receive the same NameError: name 'letterColor' is not defined error, for example. I found the solution in the PsychoPy manual:

I had to delete these two files:
~/.psychopy2/appData.cfg
~/.psychopy2/userPrefs.cfg

…which can be done by entering:
rm ~/.psychopy2/appData.cfg
rm ~/.psychopy2/userPrefs.cfg

in the Terminal (if using OSX). PsychoPy seemed to be stuck importing bad preferences each time I opened the program maybe? This solution seemed to clear up NameErrors related to referencing variables in Excel (or .csv) files. Thanks again!

Steve

1 Like

Thanks for this, after a lot of debugging was getting the inexplicable name errors which only appeared after lots of code revisions, switching between MacOSX and Windows. I couldn’t understand why the trial loop wasn’t populating/declaring the variable name that eventually presented.

Another solution that worked for me: PsychoPy Preferences -> App tab -> reset preferences and restart and open your code. This solved this strange runtime error.

3 Likes