Importing a module as code in a main program

I wanted to try out a statement made in “wxPython in Action”, which was “once your Frame class becomes complicated, you’ll probably want to move it into its own module and import it into your main program”.
As seen on the attached file:
program1.py is the original code as pulled from an online tutorial. It works fine.
layout1.py is the class MyFrame(wx.Frame): code that I pulled out of program1 and saved as layout1 in the same directory as program2.
program2 is the same as program1 except import layout1 has replaced the class MyFrame(wx.Frame): code.

The error that comes back upon running program2 is wx undefined.

I’ve tried this concept using simple python code and it worked fine but I get an error with the wx program.

Any help would be appreciated. I’ve got a lot to learn.
Thanks,
John

wxForum.txt (949 Bytes)

ps: program1 runs fine from idle.

···

On Wednesday, November 19, 2014 10:06:39 AM UTC-5, John Ruddock wrote:

I wanted to try out a statement made in “wxPython in Action”, which was “once your Frame class becomes complicated, you’ll probably want to move it into its own module and import it into your main program”.
As seen on the attached file:
program1.py is the original code as pulled from an online tutorial. It works fine.
layout1.py is the class MyFrame(wx.Frame): code that I pulled out of program1 and saved as layout1 in the same directory as program2.
program2 is the same as program1 except import layout1 has replaced the class MyFrame(wx.Frame): code.

The error that comes back upon running program2 is wx undefined.

I’ve tried this concept using simple python code and it worked fine but I get an error with the wx program.

Any help would be appreciated. I’ve got a lot to learn.
Thanks,
John

Because you haven’t imported wx in #layout1.py

···

On Wednesday, November 19, 2014 7:06:39 AM UTC-8, John Ruddock wrote:

I wanted to try out a statement made in “wxPython in Action”, which was “once your Frame class becomes complicated, you’ll probably want to move it into its own module and import it into your main program”.
As seen on the attached file:
program1.py is the original code as pulled from an online tutorial. It works fine.
layout1.py is the class MyFrame(wx.Frame): code that I pulled out of program1 and saved as layout1 in the same directory as program2.
program2 is the same as program1 except import layout1 has replaced the class MyFrame(wx.Frame): code.

The error that comes back upon running program2 is wx undefined.

add:
import wx

to the top of layout1.py and change the call from:

 **frame=MyFrame(None,-1,"Hello World")**
to
 **frame=layout1.MyFrame(None,-1,"Hello World")**

Alternatively you can leave the main program alone, but change the import call from:
import layout1
to
from layout1 import *

Thanks Rufus.
It works now by adding from layout1 import * to program2.py
and adding import wx to layout1.py
I didn’t have to change the call from:

 **frame=MyFrame(None,-1,"Hello World")**
to
 **frame=layout1.MyFrame(None,-1,"Hello World")**
···

On Wednesday, November 19, 2014 12:19:19 PM UTC-5, Rufus wrote:

add:
import wx

to the top of layout1.py and change the call from:

 **frame=MyFrame(None,-1,"Hello World")**
to
 **frame=layout1.MyFrame(None,-1,"Hello World")**

Alternatively you can leave the main program alone, but change the import call from:
import layout1
to
from layout1 import *

It works now by adding *from layout1 import ** to program2.py
and adding import *wx* to layout1.py
I didn't have to change the call from:

* frame=MyFrame(None,-1,"Hello World")
*
to
* frame=layout1.MyFrame(None,-1,"Hello World")*

that's because the "import *" brings ALL teh names in layout1 into the

current namespace. In fact, it probalby brought in "wx", as well.

But it's not generally considered good practice -- your name spaces get
cluttered and can conflict very quickly.

I highly recommend either:

from layout1 import MyFrame, some_other_name_you_need

or what was suggested, just import layout1, and then prefix your calls
with layout1.

HTH,
- Chris

···

On Wed, Nov 19, 2014 at 11:11 AM, John Ruddock <gorudd@gmail.com> wrote:

On Wednesday, November 19, 2014 12:19:19 PM UTC-5, Rufus wrote:

add:

*import wx*

to the top of layout1.py and change the call from:

* frame=MyFrame(None,-1,"Hello World")
*
to

* frame=layout1.MyFrame(None,-1,"Hello World")*

Alternatively you can leave the main program alone, but change the import
call from:

*import layout1 *
to

*from layout1 import **

--

You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I had heard this statement before in lots of forms i.e. “don’t use wildcard imports” and “namespaces are good”… but it wasn’t until I was debugging and couldn’t find a function or variable that was problematic in the file that was causing the error… and then I realized it was being imported but I had ‘blown away the namespace’ and thus didn’t have a good idea where the item was being imported from. From then on I’ve been avoiding the * imports merely to increase the speed at which I can debug, when I have a statement like myModule.myFrame(blah) I know exactly where to look if myFrame is causing problems (hint, check myModule).

···

On Wednesday, November 19, 2014 11:40:24 AM UTC-8, Chris Barker wrote:

On Wed, Nov 19, 2014 at 11:11 AM, John Ruddock gor...@gmail.com wrote:

 **frame=MyFrame(None,-1,"Hello World")**
to
 **frame=layout1.MyFrame(None,-1,"Hello World")**

It works now by adding from layout1 import * to program2.py
and adding import wx to layout1.py
I didn’t have to change the call from:

that’s because the “import *” brings ALL teh names in layout1 into the current namespace. In fact, it probalby brought in “wx”, as well.

But it’s not generally considered good practice – your name spaces get cluttered and can conflict very quickly.

Another reason not to do the * import is that when you go to bundle your application into an executable, you may end up with a lot of junk in your exe that you don’t want or need, which can make the exe bigger than it needs to be. It’s hard to exclude the parts you aren’t using if you’ve imported everything.

  • Mike
···

On Wednesday, November 19, 2014 2:40:28 PM UTC-6, Nathan McCorkle wrote:

On Wednesday, November 19, 2014 11:40:24 AM UTC-8, Chris Barker wrote:

On Wed, Nov 19, 2014 at 11:11 AM, John Ruddock gor...@gmail.com wrote:

 **frame=MyFrame(None,-1,"Hello World")**
to
 **frame=layout1.MyFrame(None,-1,"Hello World")**

It works now by adding from layout1 import * to program2.py
and adding import wx to layout1.py
I didn’t have to change the call from:

that’s because the “import *” brings ALL teh names in layout1 into the current namespace. In fact, it probalby brought in “wx”, as well.

But it’s not generally considered good practice – your name spaces get cluttered and can conflict very quickly.

I had heard this statement before in lots of forms i.e. “don’t use wildcard imports” and “namespaces are good”… but it wasn’t until I was debugging and couldn’t find a function or variable that was problematic in the file that was causing the error… and then I realized it was being imported but I had ‘blown away the namespace’ and thus didn’t have a good idea where the item was being imported from. From then on I’ve been avoiding the * imports merely to increase the speed at which I can debug, when I have a statement like myModule.myFrame(blah) I know exactly where to look if myFrame is causing problems (hint, check myModule).