Global config option - import issues again.....

Simon John simoninusa2001 at yahoo.co.uk
Sat Aug 21 23:27:56 CEST 2004


I have a program that consists of one main module and lots of small
sub-modules.

In the main module I open a text file and grep for a language setting,
this language setting will then be used as the module name of a config
file to import.

E.g. "Language=en" means config_en.py is imported as config,
"Language=fr" means config_fr.py is imported as config....

I manage this by executing a string of the language/import commmand
such as:

# execute string like "import config_en as config"
global lang_import
lang_import = "import config_" + language + " as config"
exec lang_import

Then the modules imported by the main module refer to config.CONSTANT

The problem is, that lang_import cannot be seen outside of the main
module, it's not truly global, so "exec lang_import" in the sub-modules
doesn't work.

I tried defining a method and importing that, but it doesn't work, e.g.

# main module
def lang():
import config_en as config

# sub-module
from main_module import lang;
lang()

Any ideas? Maybe some other way to import a module with a dynamic
name....




More information about the Python-list mailing list