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

S

Simon John

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....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top