# ReferenceError: os is not defined

**URL:** <https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965>\
**Category:** Online experiments\
**Tags:** pavlovia\
**Created:** [May 13, 2021, 11:44am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965 "2021-05-13T11:44:37Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 13, 2021, 11:44am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/1 "2021-05-13T11:44:37Z")

</div>

URL of experiment:  
[https://run.pavlovia.org/jonas.tvedt/gazeproject/?\_\_pilotToken=c20ad4d76fe97759aa27a0c99bff6710&\_\_oauthToken=a72848ae19ac2a14c3ddc63d93b5490500697d9647b91ac1df508a41d649c62a](https://run.pavlovia.org/jonas.tvedt/gazeproject/? __pilotToken=c20ad4d76fe97759aa27a0c99bff6710&__ oauthToken=a72848ae19ac2a14c3ddc63d93b5490500697d9647b91ac1df508a41d649c62a)

Description of the problem:

Any ideas how to resolve this? I’m guessing the problem stems from when I define a path in a code component tab, like this:

path = (“images/”)  
stimlist = [os.path.join(path, file)  
for file in os.listdir(path)  
if file.endswith(‘C.png’)]

So I figure the error arrises because os is not imported, but when I add ‘import os’ to a code component tab (before experiment), the experiment gets stuck on ‘initializing experiment…’

---

<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:** [May 13, 2021, 12:22pm UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/2 "2021-05-13T12:22:20Z")

</div>

You will need to create a spreadsheet containing the names of the files you want.

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 13, 2021, 12:38pm UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/3 "2021-05-13T12:38:01Z")

</div>

I see. Thank you. Can the spreadsheet variable be treated similarly as my variable in code? For example:

stimlist = []  
for file in _spreadsheet_:  
if file.endswith(‘C.png’)]  
stimlist.append(file)

…or something like that?

---

<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:** [May 13, 2021, 12:53pm UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/4 "2021-05-13T12:53:55Z")

</div>

> [@Jonas\_Tvedt](#):
>
> file.endswith

A quick Google suggests that you might need to change endswith to endsWith

(as well as .append to .push if you aren’t using a code\_JS from my crib sheet)

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 14, 2021, 8:20am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/5 "2021-05-14T08:20:51Z")

</div>

Thanks! But how do I reference the spreadsheet and a specific column so that it works online? Do I have to provide the xlsx file in the conditions file field of a loop or can I read it in from a code component?

---

<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:** [May 14, 2021, 8:27am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/6 "2021-05-14T08:27:57Z")

</div>

Have a look at Trial Handlers in my [coding snippets](https://docs.google.com/document/d/1kclYKgpbOCT3X8_O5pQOEIQHvD6zqMyxz3IKv3-K73I/edit#heading=h.cvmjmfh6e7dz) for Python and JS code for importing spreadsheets.

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 14, 2021, 9:56am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/7 "2021-05-14T09:56:07Z")

</div>

Great! That worked.

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 14, 2021, 10:14am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/8 "2021-05-14T10:14:14Z")

</div>

There seems to be a problem with the Trial handler method when it is used online. I get this error:

**ReferenceError: data is not defined**

I guess it comes from this line?:  
myData = data.TrialHandler()

---

<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:** [May 14, 2021, 10:17am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/9 "2021-05-14T10:17:55Z")

</div>

That’s a Python line. Did you forget to change the component to Both and edit the JS code?

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 14, 2021, 10:23am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/10 "2021-05-14T10:23:12Z")

</div>

I did not know that that was necessary. I have the component set to Auto - JS. So if I change it to Both, how should I edit the JS code to get it working?

---

<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:** [May 14, 2021, 10:26am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/11 "2021-05-14T10:26:21Z")

</div>

Have a look at Trial Handlers in my [coding snippets](https://docs.google.com/document/d/1kclYKgpbOCT3X8_O5pQOEIQHvD6zqMyxz3IKv3-K73I/edit#heading=h.cvmjmfh6e7dz) for Python and JS code for importing spreadsheets.

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 14, 2021, 11:43am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/12 "2021-05-14T11:43:54Z")

</div>

ah yes, editing the JS code according to your coding snippet got me past the error. Thanks again.

To anyone having a similar issue: you also have to specify the spreadsheet in the Additional Resources field of the Online tab in the Experiments Settings.

---

<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:** [May 14, 2021, 11:45am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/13 "2021-05-14T11:45:45Z")

</div>

> [@Jonas\_Tvedt](#):
>
> you also have to specify the spreadsheet in the Additional Resources field of the Online tab in the Experiments Settings.

Thanks for the reminder. I’ll add this note to the coding snippet page.

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 15, 2021, 8:11am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/14 "2021-05-15T08:11:53Z")

</div>

I have one more problem regarding the trialHandler method that maybe you could help me with? How do I get the values, i.e., file names, that I guess are stored in \_trialList in dictionaries? In python I could use list comprehension and refer to the column header / key like this:

```auto
stim_dict = data.TrialHandler(...)

allstim = [i['faces'] for i in stim_dict]
print('all stimuli',allstim)

```

I cant seem to find a method that works online with javascript, but I guess there must be some simple way of doing this?

---

<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:** [May 15, 2021, 8:21am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/15 "2021-05-15T08:21:46Z")

</div>

According to my code snippets you should use **aValue = myData.trialList[Idx][‘variableName’]**

```auto
allstim = []
for Idx in range(len(myData.trialList)):
      allstim.append(myData.trialList[Idx]['faces']

```

Remember to define append as push in code\_JS

However, personally I use Builder loops to push straight to lists as per my interleaved lists code.

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 15, 2021, 8:48am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/16 "2021-05-15T08:48:33Z")

</div>

Thanks! But unfortunately it gets stuck on ‘initializing experiment’ when using this code. It auto translates like this:

```auto
allstim = [];
for (var Idx = 0, _pj_a = stim_dict.trialList.length; (Idx < _pj_a); Idx += 1) {
    allstim.push(stim_dict.trialList[Idx]["faces"]);
}

```

---

<div class="post-metadata">

**Author:** ![Jonas\_Tvedt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jonas_tvedt/32/14664_2.png) [@Jonas\_Tvedt](https://discourse.psychopy.org/u/Jonas_Tvedt)\
**Post date:** [May 15, 2021, 9:07am UTC](https://discourse.psychopy.org/t/referenceerror-os-is-not-defined/22965/17 "2021-05-15T09:07:12Z")

</div>

Got past it! I used this python code:

```auto
allstim = [i['faces'] for i in stim_dict]
print('all stimuli',allstim)

```

…which was auto generated to this JS code:

```auto
allstim = function () {
    var _pj_a = [], _pj_b = stim_dict;
    for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
        var i = _pj_b[_pj_c];
        _pj_a.push(i["faces"]);
    }
    return _pj_a;
}

```

…then I added **.trialList** to the myData object manually (thanks for the clue):

```auto
allstim = function () {
    var _pj_a = [], _pj_b = stim_dict.trialList;
    for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
        var i = _pj_b[_pj_c];
        _pj_a.push(i["faces"]);
    }
    return _pj_a;
}

```
