oTree Forum >

How session variables can be configured when creating a new session?

#1 by Paw

Hi! I'm dealing with an issue that I'm unable to solve after some days: when the experimenter creates a new session in oTree, and goes to the option "configure session", and tries to customize some variables, the input numbers are not stored. It seems that the program only uses the default values given in the settings.py file (under SESSION_CONFIG_DEFAULTS), instead of taking the ones just given by the experimenter from the browser.

For example, in the attached file the customized values given by the experimenter at the moment of creating the session, are not taken into account in the file _init_.py. 

Any reason why this is happening??


#2 by Chris_oTree

Can you show a simple example of an app where this problem occurs? Please attach the __init__.py and the settings.py

#3 by Paw

Here there are. Some comments:
settings.py: The variables to customize from the browser are defined in line 32. 
_init_.py: I use the method "session_config_default" to take the value of the customized variables from settings.py

#4 by Chris_oTree

You're not supposed to import settings. See here on how to access the session config: https://otree.readthedocs.io/en/latest/treatments.html#choosing-which-treatment-to-play

#5 by Chris_oTree

By the way, the code where you read the value from session config defaults gets executed before the session is even created (you can confirm this with a print statement).

#6 by Paw

Thanks, Chris. When I try to access the session configs variables, the following error shows up: 
NameError: name 'session' is not defined

In settings.py, the configuration is: 
        display_name="Beauty Contest Game Program",

In _init_.py, I try to get the value with: GUESS_MIN = session.config['lower_bound'], defined in C.

#7 by Chris_oTree

Don't try to access the session config from the constants class. Access it from within a function. C(BaseConstants) is for things that are static and don't change between sessions.

#8 by Paw

Thanks, Chris, for your help! This finally worked.

#9 by BonnEconLab

Chris_oTree wrote: “You're not supposed to import settings.“

I would like to add that there may be situations in which it may be very useful to import some *parts* of the settings.py file. This way one can set important variables that, say, influence several apps at once, at a central location.

A use case that I have come across in my own projects is the following: I have a central parameter that influences the number of rounds in several apps. I find it highly useful to set that parameter in the settings.py file. I can, for instance, set


in settings.py and include

from settings import NUM_ROUNDS as NUM_ROUNDS_SETTINGS

class C(BaseConstants):


in the __init__.py files of the different apps. Among other things, this allows me to set NUM_ROUNDS to a low value globally during debugging.

Write a reply

Set forum username