4

I'm trying to learn wxPython and when I'm following the tutorial to learn it. I'm encountering some errors. I have done a bunch of research and can't find anything on this site that relates to my situation and I've also re installed and tried all the different versions of wxpython for python 2.7 there is still no difference. I'm on a Dell Windows 8 computer 64-bit. Here is the code from the tutorial:

import wx
class MyFrame(wx.Frame):

    def __init__(self, parent, title):
          wx.Frame.__init__(self, parent, title=title, size=(200,100))
          self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
          self.Show(True)

app = wx.App(False)
frame = MyFrame(None, 'Small editor')
app.MainLoop()

and the error:

Traceback (most recent call last):
    File "C:\Python27\test", line 2, in <module>
         class MyFrame(wx.Frame):
AttributeError: 'module' object has no attribute 'Frame'
raspberry.pi
  • 573
  • 4
  • 11
  • 21

2 Answers2

3

You have a local wx.py file in the same directory. Python imports that file instead of the wx package.

Remove or rename that file.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Ill work on getting it to work on my computer i got it to run on ubuntu vm – raspberry.pi Aug 03 '13 at 16:38
  • 2
    This is not always the problem. I have the same situation without a wx.py file – Jonathan Komar Dec 06 '13 at 18:18
  • @macmadness86: What version of Python? A *directory* by that name would also exhibit this problem. Test with `import wx; print wx.__file__`. In Python 3.3 and up, an empty directory would do this (and you'd need to test with `print(wx.__package)`), in earlier versions the directory would need to contain a `__init__.py` file. – Martijn Pieters Dec 06 '13 at 18:23
  • @MartijnPieters I am using python2.7.6. The result of print wx.__file__ is \Users\myuser\Envs\wxenv\lib\site-packages\wx.pyc. I should mention that since commenting, I installed a virtualenv to try it out there to in a fresh environment, but to no avail. Same problem. I even tried the same code on linux mint. Same problem. But on OS X Mavericks python2.7.5, it runs fine. – Jonathan Komar Dec 10 '13 at 16:09
  • @macmadness86: That is not a package, that is a single module. The `wx` project is a package (so `import wx` results in `\Users\myuser\Envs\wxenv\lib\site-packages\wx\__init__.pyc` being loaded). – Martijn Pieters Dec 10 '13 at 16:32
  • @macmadness86: I am not sure where your `wx.pyc` comes from; is there a corresponding `\Users\myuser\Envs\wxenv\lib\site-packages\wx.py` (no `c`) file there? What are its contents? – Martijn Pieters Dec 10 '13 at 16:34
  • @MartijnPieters Ok you are onto something here. My mac shows the `__init__.pyc` as it should. I did a `find / -name "wx.pyc"` on it and it lives here: `/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/py2app/recipes/wx.pyc` on my mac, but for some reason my Windows and Linux setup is finding `wx.pyc` instead of `__init__.pyc`. The contents is not readable. – Jonathan Komar Dec 10 '13 at 22:33
  • It is a bytecode cache file; the `py2app/recipes` path should probably not be on your `sys.path`. – Martijn Pieters Dec 10 '13 at 22:34
  • @MartijnPieters Ok. While that may be true, my mac runs wx fine, while Windows and Linux do not. – Jonathan Komar Dec 10 '13 at 22:36
  • Right, sorry; then that file is indeed not on your `sys.path` and not a problem. :-) – Martijn Pieters Dec 10 '13 at 22:37
  • 1
    @MartijnPieters So I took a bold leap and just deleted the wx.py and wx.pyc files on the sys.path. It worked! Now I just have to get py2exe to work and I am all set. Thanks for your help! (next up: learn how to adjust sys.path). Ultimately, you were correct about the wx.py! – Jonathan Komar Dec 10 '13 at 23:09
1

I guess you might have installed wx using pip. You may try to install wx by running the executable file from its homepage. I tried and it worked.

Ngoc Anh
  • 11
  • 1