imp module

Magnus Heino magnus.heino at
Fri Sep 29 07:20:03 CEST 2000

In article <970165987.458767 at>, "Dave Kuhlman" <dkuhlman at>

> Does the following do what you want?
> >> foo = 'aaa'  
> >> bar = 'bbb' 
> >> cmd = 'import %s.%s' % (foo, bar)
> >>  exec(cmd)

Yes. However, I realized after a couple of hours that I was doing it right all
the time ;-)

m below is bar imported, ready to use, like;

>> m.methodname()

>>> However, since I dont know the names foo and bar, I cant do it that way.
>>> Therefore I had a look at the imp module, and tried something like this:
>>> import imp fp, pathname, desc = imp.find_module('bar' ['foo'])
>>> m = imp.load_module('bar', fp, pathname, desc)
>>> What should I now do with m to get into the current namespace?

/Magnus Heino

More information about the Python-list mailing list