Function to import module to namespace

bvdp bob at mellowood.ca
Sun Jun 29 19:52:09 EDT 2008


Terry Reedy wrote:
 >

   <snip>

 >
 > Do you mean something like this?

  <snip>

 >  >>> math.__dict__.update(string.__dict__)
 >  >>> dir(math)
 > ['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',

  <snip>

I think this is working.... First off, 2 module files:

funcs.py
def func1():
     print "I'm func1 in funcs.py"

more.py
def func2():
     print "I'm func2 in 'more.py'"

and my wonderful main program:

xx.py
import funcs
def addnewfuncs(p):
     x = __import__(p)
     funcs.__dict__.update(x.__dict__)
funcs.func1()
addnewfuncs('more')
funcs.func2()

The first problem I had was getting import to accept a variable. It 
doesn't seem to, so I used __import__(). Then, I had to remember to 
assign this to a variable ... and then it appears to work just fine.

Did I miss anything in this???

Thanks for the pointer!

Bob.



More information about the Python-list mailing list