oTree Forum >

Reference to participant.vars error in creating_subsession

#1 by AlanS

Hello all,

The following is a stripped down version of my creating_subsession method:
************************************************************
def creating_session(subsession):
    session = subsession.session
    for player in subsession.get_players():
        participant = player.participant
        print('participant_id = ' + str(participant.vars['participant_id']))
***********************************************************************
        
This errors out with the following:
************************************************
Failed to create session: 'participant_id'
Traceback (most recent call last):
  File "c:\users\alans\appdata\local\programs\python\python39\lib\site-packages\otree\session.py", line 447, in create_session_traceback_wrapper
    return create_session(**kwargs)
  File "c:\users\alans\appdata\local\programs\python\python39\lib\site-packages\otree\session.py", line 418, in create_session
    func(subsession)
  File "C:\Users\alans\Documents\otree\ITQ\__init__.py", line 56, in creating_session
    print('participant_id = ' + str(participant.vars['participant_id']))
KeyError: 'participant_id'
***************************************************
The same print statement works in my live_method.  

Note: I've attached my settings.py

Thanks!

#2 by AlanS

Whoops, meant to say creating_session method!

#3 by BonnEconLab

You are referring to that variable, but have you actually ever assigned it a value?

In other words, you are trying to read the value of participant.vars['participant_id'] (participant.participant_id should also work, by the way), but I guess you have never assigned a value to that variable. The following therefore works:

def creating_session(subsession):
    session = subsession.session
    for player in subsession.get_players():
        participant = player.participant
        participant.vars['participant_id'] = "AlanS_" + str(player.id_in_subsession)
        print('participant_id = ' + str(participant.vars['participant_id']))

#4 by AlanS

Yes, I have assigned a value to it (in an upstream app).  As I said, the reference works in my live_method.

#5 by AlanS

Thanks for replying :-)

#6 by BonnEconLab

https://otree.readthedocs.io/en/latest/treatments.html#creating-session states, “creating_session is run immediately when you click the ‘create session’ button, even if the app is not first in the app_sequence.”

I guess this means that for creating_session there is no such thing as an “upstream app.”

#7 by AlanS

Oh, that sounds right.  Thank you!

Back to the drawing board on how to run this code before the live_method.  :-)

Write a reply

Set forum username