execfile fun!

Steve Thompson Steve_Thompson at maxtor.com
Fri Oct 12 09:12:08 EDT 2001


I'm trying to do something rather awkward in Python, and not having a
great deal of success at that!  I have the following modules:

#bar module (bar_module.py):
class Bar:
   def add(self, first, second):
      return first + second

bar = Bar()


#foo module (foo_module.py):
from bar_module import *
print "The sum of 23 and 45 is %d\n" %bar.add(23,45)


#main module (main_module.py):
class Main:
   def do_bar(self):
      execfile("foo_module.py")

main = Main()
main.do_bar()

While performing execfile, I get 'NameError: There is no variable
named 'bar'.  From this I surmise that the execfile statement loaded
and compiled foo, but did not import bar, as it was told.  Is there
any way of around this?

Thanks in advance and best regards to all!, 

Steve Thompson



More information about the Python-list mailing list