# Error when importing conditions in nested loops

**URL:** <https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070>\
**Category:** Online experiments\
**Created:** [June 8, 2020, 11:47am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070 "2020-06-08T11:47:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bianhaan](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bianhaan](https://discourse.psychopy.org/u/bianhaan)\
**Post date:** [June 8, 2020, 11:47am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/1 "2020-06-08T11:47:29Z")

</div>

URL of experiment: [Pavlovia](https://pavlovia.org/bianhaan/feng2013_mindwandering) (or [Sign in · GitLab](https://gitlab.pavlovia.org/bianhaan/feng2013_mindwandering) if that’s more helpful)

Description of the problem:

In case it’s helpful, I am trying to create a PsychoPy/PsychoJs version of this experiment: [OSF | Feng, S., D’Mello, S., & Graesser, A. C. (2013). Wiki](https://osf.io/56m9b/wiki/home/)

The experiment runs beautifully in PsychoPy, but I am having trouble getting it to run in Pavlovia. I seem to basically have the same problem as reported on [Psychojs - Inner Loop pattern - #8 by rrennert14](https://discourse.psychopy.org/t/psychojs-inner-loop-pattern/8087/8) and [Error When importing condition: extension currently not supported](https://discourse.psychopy.org/t/error-when-importing-condition-extension-currently-not-supported/12456).

In PsychoPy Builder my experiment looks like this:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/e/de44804d50120edee3fd82d2f55a8f4bb4e148c4.png)

Briefly, my experiment contains multiple nested loops:

- I have an outer loop (Choose\_Set) that loads a condition file SetCondition.csv. This condition file contains a column passage\_condition, listing the rows Set1\_Passages.csv and Set2\_Passages.csv
- The next loop “inward” (AllPassages) then calls $passage\_condition as condition file and should thus call either Set1\_Passages.csv or Set2\_Passages.csv. These .csv files in turn contain the columns passage\_list and comprehension\_list which list further .csv files associated with the different passages.
- These different passages are called in loops even further “inward” (passage\_loop and comprehension\_loop) which call $passage\_list and $comprehension\_list respectively which contain the actual variables called in the routines Sentence and Comprehension.

As reported before, Pavlovia appears to only import the conditions file in the outer loop and does not load the condition files in the inner loops, which means I get lots of 'undefined" conditions. I am currently completely stuck on how to to resolve this.

The javascript console gives the following error:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/8/58950850b2e9c6d92373b29d99c1426a5b91a9a5.png)

I have tried creating dummy loops as suggested in [Error When importing condition: extension currently not supported - #5 by Francesco\_Poli](https://discourse.psychopy.org/t/error-when-importing-condition-extension-currently-not-supported/12456/5), which not only made the experiment somewhat unwieldy/unelegant (given the 20 or so .csv files in total), but also did not work.

I suppose my only option is to code this. However, given my unfamiliarity with both python and javascript, I could really use some pointers (or perhaps, dare I say it, an example experiment) of where to start or how to approach this.

Any help would be very gratefully received.

Best wishes,

Bianca

---

<div class="post-metadata">

**Author:** ![rvgart](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@rvgart](https://discourse.psychopy.org/u/rvgart)\
**Post date:** [April 5, 2021, 12:39pm UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/2 "2021-04-05T12:39:03Z")

</div>

Same problem with version 2021.1.3: conditions file resourced in an inner (trials) loop seems to be found, but its single variable is “undefined”. Yet the same experiment structure, defining the conditions file in the same way, for an earlier experiment running with version 2020.1.3, and even using the same conditions file, can use its variable; it’s defined.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 5, 2021, 8:57pm UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/3 "2021-04-05T20:57:53Z")

</div>

Are you getting the same error “when importing condition X” where X is the first letter of your conditions file. If so, then the issue looks like [0] is being used on a string that you thought was a list.

---

<div class="post-metadata">

**Author:** ![rvgart](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@rvgart](https://discourse.psychopy.org/u/rvgart)\
**Post date:** [April 6, 2021, 5:49am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/4 "2021-04-06T05:49:15Z")

</div>

> [@rvgart](#):
>
> conditions file resourced in an inner (trials) loop seems to be

Actually, no, I’m not getting that error. The conditions file appears to be found/loaded; at least no error message explicitly refers to an import error. There is only the usual warning about “seed” being undefined, with no subsequent error message about importing the file. It is only when I try to use the file’s one and only variable that its value is said to be “undefined”. So, assuming that I should get an error message if the file isn’t found, or there was some “import error”, I suppose the issue is different to the one raised above apart from ending up, as this earlier post noted, ending up with “undefined conditions”.  
Trying to print the contents of the “trials” object for thisIndex seems to indicate–as below–that the variable is indeed found, with a defined value (in this case, 0), but in the next line, when I try to use the variable (here, just to print it), it’s given as “undefined”. (Other “prints” here show the file path ahead of and within the trial.). This is all based on auto-translation to JS from Python, with no problem at all running this in PsychoPy.

![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/4/d/4d13de233dffd4ad347aa7e387a1144263536388.png)

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 6, 2021, 6:49am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/5 "2021-04-06T06:49:10Z")

</div>

Having a column title undefined is indicative of it being defined twice. Try changing the name of the variable.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 6, 2021, 6:52am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/6 "2021-04-06T06:52:22Z")

</div>

I’m now wondering based on the double entry in the log whether you have TARGET as a column in both outer loop and inner loop spreadsheets

---

<div class="post-metadata">

**Author:** ![rvgart](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@rvgart](https://discourse.psychopy.org/u/rvgart)\
**Post date:** [April 6, 2021, 7:27am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/7 "2021-04-06T07:27:28Z")

</div>

Not quite - but your comment led me to what was going on, and to the following generalisation:

> You can modify the value of variable imported from a conditions file in PsychoPy without issue, but if you do so in PsychoJS, this will cause the variable to be automatically defined without a value in the script, and so undefined when you try to use it.

So I was modifying my TARGET variable, read in from the conditions file, depending on a particular condition; specifically, to swap 0 to 1 and v/v.  
`TARGET = abs(TARGET - 1)`

This left TARGET still usable (defined) in PsychoPy, but in PsychoJS, this causes the script that gets built to add the line (outside any loop or function):  
`var TARGET;`

So when I get to using TARGET, it is no longer defined for the value read in from the conditions file----at least in PsychoJS; in PsychoPy, without imposing this initial declaration, there’s no issue.

Changing the value of the modification, above, to thisTARGET, and always using that instead of TARGET, now gets it all going (for now).

Is this a bug–incompatibility between what you can do in PsychoPy but not PsychoJS?

Thanks for your pointer about there being a conflict like this at the root of the problem.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 6, 2021, 7:39am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/8 "2021-04-06T07:39:33Z")

</div>

Thanks. I hadn’t realised you could modify the value locally (because for the past year I’ve been testing everything online).

I’ll add this to my crib sheet.

---

<div class="post-metadata">

**Author:** ![rvgart](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@rvgart](https://discourse.psychopy.org/u/rvgart)\
**Post date:** [April 6, 2021, 7:51am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/9 "2021-04-06T07:51:03Z")

</div>

I suppose this could be solved–no incompatibility on this basis between PsychoJS and PsychoPy–if all the “auto-declarations” of variables in the .js script were made as early as possible; at least ahead of any TrialHandler read ins of conditions files. Because this issue only came up here because I made the modification to the conditions-file variable per trial, and so the declaration came just before “trialRoutineBegin”, well after “trialsLoopBegin” when the conditions file is read. So the declaration clobbered (undefined) the value from the conditions file.  
EDIT: No, that’s probably not right. I just suppose somehow trialsLoopBegin is called before the variables are declared.

---

<div class="post-metadata">

**Author:** ![justin88](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@justin88](https://discourse.psychopy.org/u/justin88)\
**Post date:** [April 15, 2021, 4:41am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/10 "2021-04-15T04:41:53Z")

</div>

I cant see it in the crib sheet? I cant seem to use a variable in the conditions box of a loop to import a csv ( e.g., $csv\_to\_read). Used to work last year.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 15, 2021, 6:02am UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/11 "2021-04-15T06:02:19Z")

</div>

There’s a rule (with exceptions) that if the field has a $ after its name then you don’t put a dollar in the field itself.

So, try without the $

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 6, 2023, 5:25pm UTC](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070/12 "2023-04-06T17:25:01Z")

</div>

I think I have a similar issue with my “Hello world” error, as I try to split a variable from my conditions file (a sentence into words). However, I couldn’t find it in the crib shit for some reason.
