importing modules with the same name but from different directories

Steve Holden sholden at holdenweb.com
Wed Apr 30 15:25:07 CEST 2003


"Mirko Koenig" <koenig at v-i-t.de> wrote in message
news:b80pdp$81h$1 at news.tu-darmstadt.de...
> On Mon, 21 Apr 2003 04:42:51 +0200, logistix wrote:
>
>
>
> >> -----Original Message-----
> >> From: python-list-admin at python.org
> >> [mailto:python-list-admin at python.org] On Behalf Of Mirko Koenig Sent:
> >> Sunday, April 20, 2003 9:26 PM
> >> To: python-list at python.org
> >> Subject: importing modules with the same name but from different
> >> directories
> >>
> >> I have a program consisting of many modules. Every modul has its own
> >> directory in a directory called modul, eg.
> >>
> >> .../modul/modul1
> >> .../modul/modul2
> >>
> >> 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.
>
> >>
> > Add an __init__.py file to each directory in your project.  This makes
> > the directories packages.  Then you can use a fully qualified path such
> > as "import modules.modul1.language.english as module1_english".  Note
> > that you can't use the same name for different modules at runtime, so
> > use "as xxx." to give them a more distinct name.
>
> Is it possible to to something like this:
> from modules.modul1 import *
> or
> from modules.modul1.language.english import text as modul1_text
> or
> import modules.modul1 as modul1 and than get the languag file
>

Mirko:

You seem to be able to want to do something like

if running_in_german:
    import module1.language.german as mod12lang
    import module2.language.german as mod2lang
elif running_in_English:
    import module1.language.english as mod12lang
    import module2.language.english as mod2lang
else:
    sys.exit("Don't know which language to use")

Note that dots in module names indicate "packaging": a package is a
directory, which can contain a __init__.py file to perform certain special
magic.

Another thing to try: the import of mod1lang could be handled in module1
rather than in the main program, and then the main program can simply "from
module1 import mod1lang", for example, to access variables and function and
class names bound in that module.

What you definitely *can't* do is this:

    m = "modulename_or_path"
    import m as something

To emulate this non-available solution and actually execute an import you
would need to use this:

    m = "modulename_or_path"
    exec "import %s as something" % m

I'd advise against any solution that involves exec'ing or eval'ing unless
there's really no alternative, which is not the case here. What you would
need for this is the __import__() function, built in to the language and
just waiting for you to take advantage of it.

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list