Need a hint on customizing import of modules

F. GEIGER f.geiger at vol.at
Tue Jun 8 09:55:42 CEST 2004


Hi all,

I use Leo for programming. Among other goodies Leo provides it let's me 
easily share code between different Python apps. When I have an app 
consisting of more than one file, they usually do

from other import AClassOfIt
import yetanother

When I then want to investigate a specific problem I share code with a 
lab file, e.g. LabByOpenGL.py. This app is flat then, i.e. it does not 
need to

from other import AClassOfIt
import yetanother

because all classes of other are flattened into LabPyOpenGL.py.

So I have to write within LabPyOpenGL.py

try:
    from other import AClassOfIt
except ImportError:
    pass # Assume it being w/i this module

Instantiation of AClassOfIt now works in both worlds, i.e. in the 
"structured" app and in the flat app.

But what should I do with

import yetanother ?

When code is used from yetanother, it is ref'ed like so:

myUseful = yetanother.UseFullClass()

This does not work in the flat file, because yetanother could not be 
imported.

So I thought of something like

try:
    import yetanother
except ImportError:
    yetanother = __module__ # alas, does not work

which then would make calls like

myUseful = yetanother.UseFullClass()

possible as before.

Any hint how I shall proceed? Should I consider writing an import hook? 
How would such a beast look like?

Many thanks in advance
Franz GEIGER



More information about the Python-list mailing list