[Tutor] loading modules dynamically

Girish Gajwani girishg@india.ti.com
Mon, 10 Sep 2001 22:59:18 +0530


Hi all,

I have been trying to load modules dynamically using the
__import__ hook , as follows:

#######################
#! /usr/bin/env  python

def my_import(name):
  mod = __import__(name)
  print mod
  print dir(mod)
  return mod

mod = my_import("os")
print mod.listdir(".")
#######################

My interpretation of the __import__ hook is that it loads
the module mentioned & also, aliases it to mod. Is this
correct? I could not understand the documentation mentioned
in the library reference wrt __import__, hence this
question.

Also, in what case can the above scene fail? What error
checking would I need to add here?

Regards
Girish