| Reference | Downloads | Github

Find&replace variable name

Once a script becomes more complex, one will likely need to change some variable names, in all places where these variables are called upon. Although the Coder view does not have a find&replace feature to help with this, this can still be done easily enough in an external text editor, and updating the entire text back in the Coder.

However, if one uses the Builder view, it’s unclear how this operation can be done more or less automatically. Doing it manually takes ages, since it takes many runs of the experiment to see where else the variable name appeared, which you know when you get a runtime error with the old variable name.

I tried using the “Compile to script” option, updating the variable names as described the above, and saving the .py file; however, the same script seen in the Builder view does not seem to reflect those code changes, and so one is then stuck in Coder view.

The same problem applies to updating the names of routines and components, which also appear in enough-many places that one wouldn’t want to update all those name occurrences manually.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 3.2.4

  1. Builder regenerates the .py file every time you run it, if there has been a change since the last time. So editing the .py file is a one-way street – if you want to make permanent changes to it, you have to abandon the Builder view.
  2. Fortunately, Builder .psyexp files are plain text as well (in XML format). So there is nothing to stop you from also opening that file in a plain text editor and doing a find-and-replace on it. Always work on a copy, of course, in case you make a mistake.

Thank you Michael