# String variables for filenames in Builder?

**URL:** <https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764>\
**Category:** Builder\
**Created:** [August 7, 2016, 8:12am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764 "2016-08-07T08:12:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveWalenchok](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@SteveWalenchok](https://discourse.psychopy.org/u/SteveWalenchok)\
**Post date:** [August 7, 2016, 8:12am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/1 "2016-08-07T08:12:40Z")

</div>

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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 7, 2016, 10:03am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/2 "2016-08-07T10:03:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SteveWalenchok](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@SteveWalenchok](https://discourse.psychopy.org/u/SteveWalenchok)\
**Post date:** [August 7, 2016, 5:56pm UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/3 "2016-08-07T17:56:22Z")

</div>

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:

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

```

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

Steve

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 7, 2016, 7:41pm UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/4 "2016-08-07T19:41:56Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![SteveWalenchok](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@SteveWalenchok](https://discourse.psychopy.org/u/SteveWalenchok)\
**Post date:** [August 7, 2016, 11:21pm UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/5 "2016-08-07T23:21:41Z")

</div>

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:

```python
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](http://SetupVS.py). Thanks again for your help!

Steve

---

<div class="post-metadata">

**Author:** ![SteveWalenchok](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@SteveWalenchok](https://discourse.psychopy.org/u/SteveWalenchok)\
**Post date:** [August 7, 2016, 11:58pm UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/6 "2016-08-07T23:58:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [August 8, 2016, 2:15am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/7 "2016-08-08T02:15:00Z")

</div>

> [@](#):
>
> image\_17.setImage(“resources/”+target)

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.

---

<div class="post-metadata">

**Author:** ![SteveWalenchok](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@SteveWalenchok](https://discourse.psychopy.org/u/SteveWalenchok)\
**Post date:** [August 8, 2016, 2:26am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/8 "2016-08-08T02:26:19Z")

</div>

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](http://setupVS.py) file available to Builder. Additionally, I deleted the function definition from my code in [setupVS.py](http://setupVS.py), since function calls are unnecessary for what I’m doing anyway. Thanks for all of your help!

Steve

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 8, 2016, 9:52am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/9 "2016-08-08T09:52:58Z")

</div>

Split this topic on August 8, 2016, 9:52am UTC

A post was split to a new topic: [NameError: name \_\_\_ is not defined](https://discourse.psychopy.org/t/nameerror-name-----is-not-defined/780)

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 8, 2016, 9:38am UTC](https://discourse.psychopy.org/t/string-variables-for-filenames-in-builder/764/10 "2016-08-08T09:38:06Z")

</div>

> [@SteveWalenchok](#):
>
> 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 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?)
