importing modules with the same name but from different directories

Mirko Koenig koenig at v-i-t.de
Mon Apr 21 08:44:43 EDT 2003


On Mon, 21 Apr 2003 03:52:22 +0200, Peter Hansen wrote:

> Mirko Koenig wrote:
>> 
>> In every modul directory is a diretory for the language files. These
>> files are used to display the modules label in different languages. eg.
>> 
>> .../modules/module1/language/german.py
>> .../modules/module1/language/english.py
>> .../modules/module2/language/german.py
>> .../modules/module2/language/english.py
>> 
>> My problem is:
>> If is start modul1 from within its modul directory ( .../modules/modul1
>> ) And modul1 calls modul2, then modul2 doesn't load its own language
>> file. Instead it loads the langugae file of modul1.
> 
> Although it's not 100% clear to me what you are trying to do, I'd like
> to suggest a couple of alternatives to what you seem to be trying, which
> might be simpler.
> 
> Option 1 is to just use the execfile() builtin function to execute the
> necessary files, rather than trying to use "import".
> 
> Option 2 is to rename the files to something other than .py and load
> them as regular files, then decode them with exec().

The file are named with .py because there is a dict in them, where the
text is stored, eg.
text = { 1: "hello", 2:"how are you" }

I load this dict from the needed file and have the language i want.

Because the modules are python too, i thought i don't have to use exec.
Instead i can import the module and start it from within the other modul.
Is this worse than using exec/execfile?

Mirko Koenig




More information about the Python-list mailing list