using names before they're defined

David Isaac aisaac0 at verizon.net
Wed Jul 26 13:58:52 EDT 2006


Suppose I have inherited the structure

PackageFolder/
      __init__.py
      mod1.py
      SubPackageFolder/
              __init__.py
              mod2.py
              mod3.py

When mod1 is run as a script,
I desire to import either mod2 or mod3 but not both
conditional on an option detected by the 'main()' function
in mod1.

Whichever is imported will be imported as, say, 'use_mod',
and code in mod1 will refer to use_mod.

Problem:
classes in mod1 that refer to 'use_mod' of course
raise a NameError, since the import does not take
place until 'main()' is run.

So, what is the right way to implement my desire?

Thanks,
Alan Isaac





More information about the Python-list mailing list