How to make a conditional import???

Mikael Olofsson mikael at isy.liu.se
Fri Nov 10 10:58:15 EST 2000


On 10-Nov-00 Peter Arwanitis wrote:
 >  def selectProjectType():
 >      #...    skipped
 >      if projectType == 'phys':
 >          from pyhsModule import *
 >      else:
 >          from logicModule import *

Hi, I think I have a solution. It's not the most beautiful one, but it 
should work.

def selectProjectType():
    global modulename
    #...
    if projectType == 'phys':
        modulename = 'pyhsModule'
    else:
        modulename = 'logicModule'

Later, when you select the project type:

selectProjectType()
exec('from '+modulename+' import *')

Good luck!

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    10-Nov-00
Time:    16:53:26

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------



More information about the Python-list mailing list