Import function in Javascript does not work

URL of experiment:

Description of the problem:

Hi everyone! I had the following code in JS:

import {normal} from ‘numpy/random’;
var coinxlocation, coinylocation, splash1xlocation, splash1ylocation, splash2xlocation, splash2ylocation, splash3xlocation, splash3ylocation, splash4xlocation, splash4ylocation, splash5xlocation, splash5ylocation;
coinylocation = normal({“loc”: 0, “scale”: 0.1});
coinxlocation = normal({“loc”: 0, “scale”: PriorSD});
splash1ylocation = normal({“loc”: 0, “scale”: 0.1});
splash2ylocation = normal({“loc”: 0, “scale”: 0.1});
splash3ylocation = normal({“loc”: 0, “scale”: 0.1});
splash4ylocation = normal({“loc”: 0, “scale”: 0.1});
splash5ylocation = normal({“loc”: 0, “scale”: 0.1});
splash1xlocation = normal({“loc”: coinxlocation, “scale”: LikelihoodSD});
if ((splash1xlocation > 0.79)) {
splash1xlocation = Math.min(0.79, splash1xlocation);
}
if ((splash1xlocation < (- 0.79))) {
splash1xlocation = Math.max((- 0.79), splash1xlocation);
}
splash2xlocation = normal({“loc”: coinxlocation, “scale”: LikelihoodSD});
if ((splash2xlocation > 0.79)) {
splash2xlocation = Math.min(0.79, splash2xlocation);
}
if ((splash2xlocation < (- 0.79))) {
splash2xlocation = Math.max((- 0.79), splash2xlocation);
}
splash3xlocation = normal({“loc”: coinxlocation, “scale”: LikelihoodSD});
if ((splash3xlocation > 0.79)) {
splash3xlocation = Math.min(0.79, splash3xlocation);
}
if ((splash3xlocation < (- 0.79))) {
splash3xlocation = Math.max((- 0.79), splash3xlocation);
}
splash4xlocation = normal({“loc”: coinxlocation, “scale”: LikelihoodSD});
if ((splash4xlocation > 0.79)) {
splash4xlocation = Math.min(0.79, splash4xlocation);
}
if ((splash4xlocation < (- 0.79))) {
splash4xlocation = Math.max((- 0.79), splash4xlocation);
}
splash5xlocation = normal({“loc”: coinxlocation, “scale”: LikelihoodSD});
if ((splash5xlocation > 0.79)) {
splash5xlocation = Math.min(0.79, splash5xlocation);
}
if ((splash5xlocation < (- 0.79))) {
splash5xlocation = Math.max((- 0.79), splash5xlocation);
}
Coin.setPos([coinxlocation, coinylocation]);
Splash1.setPos([splash1xlocation, splash1ylocation]);
Splash2.setPos([splash2xlocation, splash2ylocation]);
Splash3.setPos([splash3xlocation, splash3ylocation]);
Splash4.setPos([splash4xlocation, splash4ylocation]);
Splash5.setPos([splash5xlocation, splash5ylocation]);

There was an error on the first line [import {normal} from ‘numpy/random’] which caused me stuck on “initialising the experiment”.

I looked through the cribe sheet which suggested "remove all references to avoid the experiment crashing while ‘initialising the experiment’ ". But I am bit confused about what that means in my scenario. Any help would be appreciated!

Thanks in advance!
Luisa

Hello,

With import numpy.random you import a Python-library in a JavaScript-program. This will not work. Online you are not running Python-code but JavaScript. So delete the code-line in which you import any Python-libraries and your code should run online. If you are using the Builder to program your experiment, all Python-libraries that have a JavaScript equivalent are imported.

Best wishes Jens

Hi Jens, thank you for the help.

I deleted “import {normal} from ‘numpy/random’;”, but I still got the same error “Uncaught syntax error: unexpected error” on the same code-line.

I also tried deleting “import {normal} from ‘numpy/random’;
var coinxlocation, coinylocation, splash1xlocation, splash1ylocation, splash2xlocation, splash2ylocation, splash3xlocation, splash3ylocation, splash4xlocation, splash4ylocation, splash5xlocation, splash5ylocation;”, but same error message again.

Hello Luisa

I meant deleting it from your PsychoPy file. Did you do that? Does your experiment run offline?

An uncaught syntax error can mean a lot, a missing semicolon, a misspelled command, missing parentheses aso. The error does not have to be on the line that is reported. The error can occur earlier in the code.

Best wishes Jens

Hello Jens, I deleted it in the JS file. Did you mean delete it in the builder mode of PsychoPy?

Hello Luisa

Yes, I meant delete the line from your PsychoPy file, not the PsychoJS file. PsychoPy rewrites the PsychoJS file every time you sync with your project. So the import line will be reintroduced every time you sync.

Best wishes Jens

1 Like

oh I see, thank you so much!