Module import problems

simo simoninusa2001 at yahoo.co.uk
Sun Apr 4 21:25:36 EDT 2004


I've written a pretty big wxPython script, and I thought I'd split the
source into a few files.

I'm going to have a main.py file which includes global defs, wxApp
initialisation code, mainWindow() etc. and then each file will include
a GUI class (which just happen to be a wxNotebook tab each).

I'm having trouble accessing the imported classes though.

For example I have login.py which contains something like this:

class login(wx.Dialog):
    """create a login dialog box"""
    def __init__(self):
        wx.Dialog.__init__(self, None, -1, "Login")

        # create grid sizer     
        self.grid_sizer = wx.GridSizer(3, 2, 2, 2)

Then main.py contains something like this:

import login

class MyApp(wx.App):
    """define main wx app class"""
    def OnInit(self):
        login_frame = login()
        login_frame.Show(1)
        return True

# call the main class
app = MyApp(0)
app.MainLoop()

The problem is, that when I run main.py, I cannot seem to import login
as a class - I keep getting 'module' object not callable.

I tried login.login() to not call the module, but the class of the
module, but that doesn't work either, I get 'module' object has no
attribute 'login'. I thought login.login() would look for the def
'login' not the class anyway?

So how do you import classes from other files in Python - I can only
seem to get it to import defs using 'from login import *' (yuk) or
'import login', but I need classes for wxPython.

Am I looking at this wrong, it is a bit of a Perl approach? Should I
be calling login.init() or something weird?

I always seem to get this with Python, I do pretty well, then I try to
tidy things up and it gets too fiddly, I guess I still haven't got my
head around OOP....



More information about the Python-list mailing list