473,505 Members | 16,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Module import problems

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....
Jul 18 '05 #1
5 1921
simo wrote:
I'm having trouble accessing the imported classes though.

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

class login(wx.Dialog): ....
Then main.py contains something like this:

import login

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

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?


login.login(), based on the precise code that you showed, appears
to be the correct thing. I'm not actually trying to understand
how your code works, mind you, just looking at it syntactically.

If that's giving you the error you say it is, something else is
amiss. Do you "shadow" the "login" that comes from the import
statement with a function somewhere, or a method, in the class
MyApp, which you didn't show? You should be able to use the
interactive prompt to "import login" and "login.login" without
getting any kind of "no attribute" error. If you can do that,
clearly the problem is in the MyApp module. Otherwise you've
got something wrong with login.py after all.

-Peter
Jul 18 '05 #2
login.login or login.login() don't work.

I tried adding...

def simon():
print "hello"

....to the top of login.py (before the login class) and then calling
login.simon() and that works - but then that's a def, not a class....

So I think the login class is failing to initialise as it can't find
the wx class imported in main.py, because if I run login.py then I get
"...wx not defined" surely I don't have to import wx in every file
(that doesn't work actually)?!

This namespace crap is becomming an inconvenience - jees, do we have
to make everything global?!

Also, does IDLE cache the compiled Python or something, as sometimes
when I change a file and save/run it in the shell, it doesn't change
(I do delete the .pyc files) until I quit and reload IDLE?

This is with Python 2.3.2/Solaris (ActiveState) and 2.3.3/Windows
(python.org) and wxPython 2.4.2.4/2.5.1.5
Jul 18 '05 #3
simo wrote:
So I think the login class is failing to initialise as it can't find
the wx class imported in main.py, because if I run login.py then I get
"...wx not defined" surely I don't have to import wx in every file
(that doesn't work actually)?!
Yes, of course you do.
This namespace crap is becomming an inconvenience - jees, do we have
to make everything global?!
Really, it's not at all that difficult. Anyway, the namespaces are
there to protect you and simplify the code. Although you could think
of it as a small inconvenience to have to put imports for various
things wherever you need them, the alternative is vastly more of a
problem.
Also, does IDLE cache the compiled Python or something, as sometimes
when I change a file and save/run it in the shell, it doesn't change
(I do delete the .pyc files) until I quit and reload IDLE?


I never use IDLE, so I can't say. My preferred approach is simply
running things from the command line (but you get that way when you
start focusing on unit testing rather than code-and-fix...).

-Peter
Jul 18 '05 #4
Yay, I've done it!

I had to resort to "from login import *" in main.py and then "import
wx" (and other modules like urllib/webbrowser etc) in login.py and it
all seems to work.

I didn't occur to me that I'd have to import the modules used in each
module file, I thought it would work just from the parent (main.py)
file.

I've also stopped myself from calling classes instead of defs (I had
the whole class in the __init__ def previously) and removed most of my
global variables.

Now I have twelve 1-6Kb files like login.py, plus an 18Kb main.py,
instead of one 38Kb main.py to grep through!

So it was worth the struggle to clean up a bit....
Jul 18 '05 #5
> I had to resort to "from login import *" in main.py and then "import
wx" (and other modules like urllib/webbrowser etc) in login.py and it
all seems to work.
Sounds ugly. I would stick with...

#in main.py
import login
import wx

#in login.py and others...
import wx

I didn't occur to me that I'd have to import the modules used in each
module file, I thought it would work just from the parent (main.py)
file.
You were misunderstanding modules, namespaces, etc.

I've also stopped myself from calling classes instead of defs (I had
the whole class in the __init__ def previously) and removed most of my
global variables.
What do you mean "calling classes"? Show an example of what you were
doing before, and what you are doing now, and I'm sure someone will tell
you which is more "Pythonic" and/or which is a better design.

Now I have twelve 1-6Kb files like login.py, plus an 18Kb main.py,
instead of one 38Kb main.py to grep through!

So it was worth the struggle to clean up a bit....


Congrats.

- Josiah
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1292
by: alejandro david weil | last post by:
Hello! I got the next problem and didn't see any reference to the behaviour that produces it, look: If we have a module like: --- mod.py --------------->8------ testvar2 = 15
3
3802
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo...
9
5006
by: ajikoe | last post by:
Hello, I have two modules (file1.py and file2.py) Is that ok in python (without any weird implication) if my module import each other. I mean in module file1.py there exist command import file2...
25
7717
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
9
1218
by: noamsml | last post by:
Hi, I am new to python and am currently writing my first application. One of the problems I quickly ran into, however, is that python's imports are very different from php/C++ includes in the...
11
1480
by: Jim | last post by:
Hi, I have created an import module. And would like to access a function from the main script, e.g., file abc.py: ################### def a(): m() return None
6
1714
by: ai | last post by:
It assumes that there is a module A which have two global variables X and Y. If I run "import A" in the IDLE shell, then I can use A.X and A.Y correctly. But if I want to change the module A and...
4
2185
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
3
1447
by: Jugdish | last post by:
Why doesn't the following work? $HOME/pkg/__init__.py $HOME/pkg/subpkg/__init__.py $HOME/pkg/subpkg/a.py $HOME/pkg/subpkg/b.py # empty import a
0
974
by: Jacob Davis | last post by:
On Apr 3, 2008, at 10:54 AM, Trent Mick wrote: Thanks, that seems to have worked. I added "/usr/local/bin" to the PATH in the preferences Environment panel in Komodo. Then in preferences I...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.