usage of __import__ across two files

bwobbones bwobbones at gmail.com
Sun Dec 12 09:35:36 EST 2004


Hi,

  I'm having trouble making __import__ work with the two classes 
attached. The PrintHello() method can't be seen in the BMTest2 class - 
what am I doing wrong here?

****************************
class one - BMTest - in BMTest.py:
****************************
import wx
from traceback import print_exc

class ImportTest(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "ImportTest",
                          size = (666,480), style = wx.DEFAULT_FRAME_STYLE)
        #tb = BMToolBar(self) # works just fine!
        tb = __import__('BMTest2')
        tb2.PrintHello()


class MyApp(wx.App):
    def __init__(self, flag):
        wx.App.__init__(self, flag)
    def OnInit(self):
        frame = ImportTest()
        self.SetTopWindow(frame)
        return True
       
if __name__ == '__main__':
   try:
       app = MyApp(False)
       app.MainLoop()
   except:
       print print_exc()

**************************
class 2 BMTest2 - in BMTest2.py:
**************************
import wx

class BMToolBar(wx.ToolBar):
    def __init__(self, parentFrame):
        wx.ToolBar.__init__(self, parentFrame, -1, 
style=wx.TB_HORIZONTAL|wx.NO_BORDER|wx.TB_FLAT|wx.TB_TEXT)
        print "*** gday ***"
        self.Realize()
       
    def PrintHello(self):
        print "Hello"

Any help will be much appreciated!

Bones



More information about the Python-list mailing list