Need a hint on customizing import of modules

F

F. GEIGER

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top