psychopy.org | Reference | Downloads | Github

String variables for filenames in Builder?

Hi everyone,

Sorry for the double-post; I overlooked the new forum link in the old Google group page. I’m new to PsychoPy and Python, and I’ve been having an issue with filenames in Builder. Basically, I’ve been trying to create a simple visual search experiment. I’ve created a separate .py file in Coder that randomizes stimuli and locations, and holds various image filenames in string variables (I double-checked with print and the code works). In Builder view, I import the .py file at the start of the Trial routine with a Code Component. (I’ve also tried just posting all of the code from the .py file in the Code Component.)

Within each trial, I’ve added a series of “placeholder” images. However, for each placeholder image, when I try to define my filenames with string variables in the “Properties” menu next to “Image”, I always get a “Couldn’t find image file” error. I’ve tried a variety of formats, e.g., Resources/target, “Resources/”+=target, etc., where “Resources” is the relative path to the folder and “target” is an example string variable holding a filename. I’ve found a lot of answers to similar issues regarding filenames in Builder, but all seem to suggest either explicitly defining the path or referencing a .csv. I’m familiar with defining image files in a similar way in E-Prime, but maybe this is a feature that Builder doesn’t have? Any help is greatly appreciated!

Thanks,

Steve

System:
OSX 1.11.5
PsychoPy 1.83.04, Standalone

Here’s the full error:

3.3523 	ERROR 	Couldn't find image file '"Resources/"+target'; check path?
2016-08-07 10:47:44.431 python[6373:852261] 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 508, in <module>
    image_17.setImage(u'"Resources/"+target')
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/visual/image.py", line 261, in setImage
    setAttribute(self, 'image', value, log)
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/tools/attributetools.py", line 100, in setAttribute
    setattr(self, attrib, value)  # set attribute, calling attributeSetter if it exists
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/tools/attributetools.py", line 20, in __set__
    newValue = self.func(obj, value)
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/visual/image.py", line 249, in image
    maskParams=self.maskParams, forcePOW2=False)
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/visual/basevisual.py", line 647, in _createTexture
    % (tex, os.path.abspath(tex))#ensure we quit
OSError: Couldn't find image file '"Resources/"+target'; check path? (tried: /Users/Steve/Dropbox/Research and Course Files/Software/PsychoPy Stuff/Visual Search Sample/"Resources/"+target)

Please edit your post to contain the full error message (you can copy with right-click from the pop-up window). That way, when someone searches for that error message they’ll more likely find you post.

That said, the most common reason for PsychoPy not finding an image is that it actually ends with an extension (.jpg, .png or .tif) and you haven’t included that in your filename (because OSX and win32 hide it in their file view).

PsychoPy can certainly handle string variables as paths to images. Paths should be relative to the experiment file and it looks like you’re doing that. If you post the entire error message then we would have more chance of guessing what has gone wrong in your path format though.

1 Like

Sorry about forgetting the error message! I’ve edited the post to include the full message.

When defining the string variable, I’ve included the .bmp extension, and when I print the stimulus list in the .py file, everything looks good. Here’s how I defined the strings in the .py file:

target = competitors[22]
target = target + ".bmp"
...

My syntax is probably off in the Builder. Thanks for the quick reply!

Steve

OK, yes the error message made it clear. Look carefully at the last line, where it says the path it tried it has literally the value that you put in as the path (including an extra pair of quotes) rather than the variable you were trying to represent.

That means it’s looking for a file literally called “target”. To make Builder understand that your entry is code, not a literal string you need to add the dollar sign:

$"Resources/"+target

Thank you very much for your help! Adding the “$” works beautifully when I past all of my custom code directly into the Code Component (at Begin Routine).

However, I have one more hurdle: I’m trying to have a portable, separate, custom “.py” file within the experiment directory, import that file in the Code Component in Builder (at Begin Experiment), and call a function within .py file at Begin Routine. However, when I try this, I get a NameError:

Running: /Users/Steve/Dropbox/Research and Course Files/Software/PsychoPy Stuff/Visual Search Sample/visualsearch_lastrun.py 
**beard.bmp**
2016-08-07 16:11:38.816 python[9303:973770] 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 303, in <module>
    image_17.setImage("resources/"+target)
NameError: name 'target' is not defined

As you can see in bold, I tested the custom code with print, and the target filename exists! It’s not getting to the image, however. If this helps, here is my code in Begin Experiment:

import setupVS

and at Begin Routine:

setupVS.stimuli()

where “stimuli” is the function in SetupVS.py. Thanks again for your help!

Steve

Update: I just caught that I forgot a return statement. However, returning the target string at the end of the function still results in the exact same NameError.

-Steve

Hi Steve, We don’t know where that line comes from (is it your own code or something Builder generates?). Either at that point, target has not yet been defined, or the code is in your function and you would need to pass in target as a parameter to the function so that it knows about it.

Sorry about being unclear: that code was generated by Builder, with “target” being the name of the string variable from my code holding the target’s filename.

After much trial and error, I just got it to work a few minutes ago: In the Code Component prior to each trial, under Begin Routine, I inserted from setupVS import *. This seems to make all variables in my custom setupVS.py file available to Builder. Additionally, I deleted the function definition from my code in setupVS.py, since function calls are unnecessary for what I’m doing anyway. Thanks for all of your help!

Steve

A post was split to a new topic: NameError: name ___ is not defined

This is really a separate issue that needs a different topic (you’re no longer talking about strings as filenames). Please help people find specific answers to specific questions by asking them in

But FYI: If you do import setupVS then you would access target by using setupVS.target whereas from setupVS import * puts them in you root “namespace” (but beware that it more likely leads to name clashes, right?)