Pavlovia error : Failed to resolve module specifier "psychopy"

URL of experiment: training [PsychoPy]

Description of the problem:

Hello everyone, although I’ve seen similar issues on the forum, I can’t find an answer to my question. I created a training in PsychoPy (version 2024.1.5), and I would like to publish it on Pavlovia. However, I encounter the following error: Uncaught TypeError: Failed to resolve module specifier “psychopy”. Relative references must start with either “/”, “./”, or “…/”.

This corresponds to line 1 of my file, the first line being the title, I assume it refers to the first effective line which is as follows:
import { core, data, sound, util, visual, hardware } from './lib/psychojs-2024.1.5.js';

Before coming here to report my issue, I asked ChatGPT, and I deduced that there is an error in the HTML export of the experiment, because there should supposedly be a lib/ folder created, which is not the case for me.
I am somewhat stuck and don’t know what to do to resolve this issue.

Hello

The error come from this row

// Start code blocks for ‘Before Experiment’
// Run ‘Before Experiment’ code from code_renforcement
import {event} from ‘psychopy’;
core = psychopy.core;

What code do you have in a Before Experiment tab? Could you move the code to a Begin Experiment tab?

Best wishes Jens

1 Like

Please could you show the contents of your GitLab folder? You certainly shouldn’t have a lib folder in it. I think the issue might be that you don’t have JavaScript files in it either. Possibly you do, with the wrong name.

Hey, thank you for your answer. Here is my Before Experiment code, I think I can put it in Begin experiment

from psychopy import visual, event import psychopy.core core = psychopy.core # Ensures that core is the module and not a local variable

I’m figuring out that I don’t have anything on GitLab. I thought it was automatic (oops!). I’ll add it and get back to you!

It is automatic if you have Export HTML on Sync in Experiment Settings / Online

Hello @Clemence

This could indicate that your experiment did not compile correctly into PsychoJS.

Are you using the Builder to program your experiment? Do you mind showing Python code to PsychoJS code instead?

Best wishes Jens

I have it, and I have also done ‘File > Export HTML’, but still nothing on GitLab.

Yes, I am using the Builder to design my experiment. Since your response, I’ve been trying to extract my Python code (without running the full experiment), but for some reason, PsychoPy is not letting me export it as a .py file. Also, the Coder doesn’t display anything unless I export the experiment as HTML for the JavaScript version. I will get back to you once I find a solution for the Python export.

Hello @Clemence

just show the content of the code-components that you programmed.

Best wishes Jens

Okay, thank you, here it is !
My first code is in “Info_Imc” routine :
→ Begin Experiment : from psychopy import visual win.mouseVisible = True IMCpercu = None # Initialisé à None delta = 5

→ End Routine : thisExp.addData('textbox_IMC', textbox_IMC.text)
try: IMCpercu_text = textbox_IMC.text # Récupère le texte de la textbox IMCpercu = float(IMCpercu_text) # Convertit en float

# Vérifie que l'IMC perçu est positif
if IMCpercu <= 0:
    raise ValueError("Les valeurs doivent être positives.")

# Sauvegarde l'IMC perçu dans les données
thisExp.addData('IMCpercu', IMCpercu)

except ValueError as e: print(f"Erreur de conversion : {e}") # Affiche un message d'erreur dans la console IMCpercu = None # Optionnel : gérer une valeur par défaut en cas d'erreur

In “essai” routine :
→ Begin routine :
# Calculer le seuil de l'IMC cible basé sur l'IMC perçu imc_target = IMCpercu + delta

# Déterminer la réponse correcte en fonction de la condition if condition == "plus_élevée": correct_response = 'v' if stim_imc > imc_target else 'f' # Comparaison avec l'IMC cible (IMC perçu +5) elif condition == "plus_faible": correct_response = 'v' if stim_imc < imc_target else 'f' # Comparaison avec l'IMC cible (IMC perçu +5)

# Enregistrer la réponse correcte attendue thisExp.addData('CorrectResponse', correct_response)

# Afficher les valeurs pour vérification print(f"IMC perçu: {IMCpercu}, IMC cible: {imc_target}, IMC silhouette: {stim_imc}, Condition: {condition}") print(f"Réponse correcte: {correct_response}")

→ Each Frame :
# Récupère les touches appuyées keys = event.getKeys(keyList=['v', 'f']) # 'v' pour vrai, 'f' pour faux

if keys: participant_response = keys[0] # Première touche pressée # Vérifie si la réponse est correcte print(f"Réponse du participant: {participant_response}, Réponse correcte attendue: {correct_response}") if participant_response == correct_response: print("Bonne réponse !") feedback_message = 'Correct' reinforcement = "positif" else: print("Mauvaise réponse.") feedback_message = 'Incorrect' reinforcement = "négatif"

# Enregistre la réponse dans les données thisExp.addData('ParticipantResponse', participant_response) thisExp.addData('Reinforcement', reinforcement)

continueRoutine = False  # Passe à l'essai suivant

In “feedback” routine :
→ Begin experiment : from psychopy import visual, event import psychopy.core core = psychopy.core progress = 0.5

→ Begin routine :
step = 0.05
# Déterminer le feedback à afficher if reinforcement == "positif": feedback_text.setText("Correct !") feedback_text.setColor('green') progress = min(progress + step, 3.0) # Limite à 100% barColor = "green" elif reinforcement == "négatif": feedback_text.setText("Incorrect !") feedback_text.setColor('red') progress = max(progress - step, -3.0) # Limite à 0% barColor = "red"

# Afficher les éléments (affichage automatique activé) feedback_text.setAutoDraw(True)

# Durée du feedback feedback_duration = 2.0 feedback_clock = core.Clock() feedback_clock.reset()

→ Each Frame :
# Fin du feedback après le délai spécifié if feedback_clock.getTime() >= feedback_duration: feedback_text.setAutoDraw(False) continueRoutine = False # Arrêter la routine après 2 secondes

Hello

Do I understand your post correctly that you import libraries via code components? If so, it won’t work online. Delete all import commands from your routines. I saw import in the routine Info_lmc and feedback. The Builder includes all Python libraries that have PsychoJS counterparts.

Get rid of the print commands in Each frame when your experiment is finished. print only prints to the console and will slow down your experiment.

By the way, if you enclose your code in triple `, it will be formatted correctly.

Best wishes Jens

1 Like

Thank you so much! I did it, and it partially solved my problem!
Also, sorry for not using triple `, I’ll be careful next time!
Pavlovia now starts running the experiment, but it shows the following error:

Unfortunately, we encountered the following error:

  • When setting the image of ImageStim: image_sil
  • When getting the value of the resource: stimuli\IMC_15_nu.png
  • Unknown resource

And here is what appears in the console:

Is it because I don’t have anything on Gitlab ?

1 Like

Thank you! Do I need to change the slashes anywhere else besides my .xlsx file?

ALL file paths

Well… I think I did? I don’t see anything else in the Builder or Code Components that would have backslashes. Also, it looks like this in my JavaScript:

If it still isn’t working use an incognito tab and developer tools to check/show the errors.

For some reason, it works in an incognito tab without error messages. However, the experiment does not function properly. Referring to my previous image of the builder, it completely skips the ‘essai’ routine and gives random feedback, mostly negative. The incognito tab only shows warnings. Additionally, how can I make it work in normal tabs as well? Thank you so much for your time.

Update : it works on normal tab, but the warnings are the same, and the full “essai” routine is skipped too.

Hello @Clemence

That will not be a problem. You have been running the experiment from the browser‘s cache. Clear your cache and the experiment will run fine.

Well, that is another problem. Do you mind posting your code properly formatted? Is there a reason why you check the correctness of the response in each frame? Does the keypress end the routine? Did you define the correct key in your condition-file?

Best wishes Jens