Python variable scope between modules

I have some serious lack of knowledge on how variables in python work. My code is spread between multiple modules and I will try to write only the more important stuff:

import math

LAMBDA = 0.03
EPSILON = 0.97
SCALING_PARAMETER = 10  # Used for initializing the propensities
PRICE_SET = list(range(math.floor(FEED_IN_TARIFF), 
math.floor(UTILITY_PRICE) + 1))

probabilities = [1 / len(PRICE_SET)] * len(PRICE_SET)

def algo(p_local_sell=0, p_grid_sell=0, p_local_buy=0, cl_price=0):

    if p_local_sell != 0 or p_grid_sell != 0:
        r_prosumer = p_local_sell * cl_price + p_grid_sell * FEED_IN_TARIFF
    elif p_local_buy != 0:
        r_consumer = p_local_buy * abs(FEED_IN_TARIFF - cl_price)

    prop_i = PRICE_SET.index(cl_price)

    if p_local_sell != 0 or p_grid_sell != 0:
        propensities[prop_i] = (1 - LAMBDA) * propensities[prop_i] + r_prosumer * (1 - EPSILON)
    elif p_local_buy != 0:
        propensities[prop_i] = (1 - LAMBDA) * propensities[prop_i] + r_consumer * (1 - EPSILON)

    for i in range(0, prop_i):
        propensities[i] = (1 - LAMBDA) * propensities[i] + (propensities[i] * EPSILON) / (len(PRICE_SET) - 1)
    for i in range(prop_i + 1, len(propensities)):
        propensities[i] = (1 - LAMBDA) * propensities[i] + (propensities[i] * EPSILON) / (len(PRICE_SET) - 1)

    for i in range(0, len(probabilities)):
        probabilities[i] = propensities[i] / sum(propensities)

import price
import numpy as np

def update_data(data):
    # some unimportant calculations

def get_price():
    # some unimportant calculations
    price = np.random.choice(price.PRICE_SET,1,p=price.probabilities)
    return int(price[0])

The code is in docker containers and when I execute the update_data(data) function, and docker exec -it "container_name" python3 -c "import price; print(price.propensities)" it returns always the initial ones.

  1. Am I checking the values wrong, when I try to print the values within the containers?
  2. Do the variables have to be global for this to work?
  3. Is my understanding so poor, that what I am trying to do is not possible and I would be better off when I write the values into an external file and read them from there?


Your docker exec command starts a new Python process, imports the module, and then prints the value. Of course the value is the default, because you just imported the module and did nothing with it in that Python process.

This is not a problem of module scope, but of process scope. If you want to get a value from a process that is already running, you would typically send some kind of request there, which means that the process would have to be a server that responds to requests. Maybe there are ways to use debugging interfaces to connect to a running process, but that’s not something I’d recommend for anything but interactive debugging.

If you’re only interested in certain values, and these change only at well-defined times, you could let the process write the values to a file every time they change, and then read the file from docker exec. But then you’d still have to guard against the case that the file is being overwritten while you’re reading it.

Leave a Reply

Your email address will not be published. Required fields are marked *