Python Forum
Sharing variables across modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sharing variables across modules
#1
Hey all,
today I somehow managed to get stuck with getting a variable in one module changed by a function in another module - something I had working last time I wrote multi-module scripts.

Simplifying the code (otherwise requiring Kivy to run), I have:

main.py:
from kivy.app import App
import parameters
import utils


class MainApp()
    def on_start(self): # runs when application starts
        utils.read_config_file() # reads config text file and should modify parameters dictionary accordingly
        Window.clearcolor = hex2color(parameters.parameters["background_color"]) # should use new color value instead of default one

if __name__ == '__main__':
    MainApp().run()
parameters.py:
# dictionary with default values
parameters= {
    "config_file": "config.txt",
    "width": "800",
    "height": "400",
    "background_color": "#FFFFFF",
}
utils.py
import parameters


# reads config.txt file and should overwrite default values in parameters dictionary with those found in file
def read_config_file():
    with open(parameters.parameters["config_file"], "r") as config_file:
        for line in config_file:
            if line == "\n" or line == "":
                continue
            key, value = line.strip().split() 
            parameters.parameters[key[:-1]] = value.strip()       
I would like the parameters dictionary (in parameters.py module) to be referenced from other modules, so the variables can be shared.
First on application startup, a configuration text file is to be read and values in parameters dictionary overwritten with those found in file.
However, the values in dictionary are not affected by read_config_file() function in utils.py. The values from file are read correctly though.

What am I missing? Is there some variable/module naming conflict I overlooked? Am I doing the imports incorrectly?
Thanks for hints,
JC
Reply


Messages In This Thread
Sharing variables across modules - by j.crater - Jul-29-2018, 11:00 PM
RE: Sharing variables across modules - by j.crater - Jul-30-2018, 07:20 AM
RE: Sharing variables across modules - by j.crater - Jul-30-2018, 09:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to secure API key when sharing quarinteen 2 460 Jan-19-2024, 04:46 PM
Last Post: deanhystad
  Sharing imported modules with Sub Processes? Stubblemonster 2 1,603 May-02-2022, 06:42 AM
Last Post: Stubblemonster
  How to install modules for 2.7 (or sharing from 3.8)) Persisto 2 2,614 Dec-31-2021, 02:33 PM
Last Post: Persisto
  multiprocessing and sharing object cyrduf 0 2,124 Feb-02-2021, 08:16 PM
Last Post: cyrduf
  sharing variables between two processes Kiyoshi767 1 1,959 Nov-07-2020, 04:00 AM
Last Post: ndc85430
  Sharing my code emirasal 2 2,136 Oct-04-2020, 02:21 PM
Last Post: emirasal
  Sharing X Axis in Sub plots JoeDainton123 1 2,275 Aug-22-2020, 04:11 AM
Last Post: deanhystad
  Instances sharing attributes midarq 4 2,595 Sep-20-2019, 11:13 AM
Last Post: midarq
  Use of global variables from several modules. Jstechg 3 2,913 Jan-03-2019, 03:39 AM
Last Post: scidam
  Class Modules, and Passing Variables: Seeking Advice Robo_Pi 21 10,829 Mar-02-2018, 05:22 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020