Image

Imagesej7278 wrote in Imagepython_dev

Passing variables around multiple modules

I'm trying to add a language choice to my application, which would then be used to import an english.py file or maybe use ConfigParser to parse a french.ini file.

I'm not sure if I'm going to have a choices.txt file or a Registry entry or what (Registry is not really an option as I don't want to require Admin access, or Windows for that matter) for storing the language.

Anyway, I have the main script, which imports other scripts, which need to know the configured language. Now of course a global variable is only global in one module.

So how do I pass the language around throughout the scripts - I don't really want to have to read the choices.txt/Registry in every module, and ideally I don't want to read/import the french.ini/english.py in every module either.

Currently I have french.py and english.py and I rename the one I want to use to language.py and import it in every module, then refer to language.CONSTANT - but that only works at build time, obviously you can't do that at runtime (I'm using py2exe/cx_Freeze too).

It's a shame you can't pass a list of variables to the import method, and have them passed to the imported module - kinda like you can when __init__()'ing class. I guess I could overload import() with my own method, but that's nasty.

Basically, I need to pass a language variable around through any imported scripts and the main script.