17

I just downloaded wxPython, and was running some of the sample programs from here. However, on every line that uses a variable from wx.*, I get a "Undefined variable from import error"

For example, the following program generates five errors on lines 1,4,8, and two on line 5:

import wx
class MyFrame(wx.Frame):
    """ We simply derive a new class of 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()

The program, however, compiles and runs perfectly. I haven't made any significant modifications to pydev or eclipse, and the wxPython install is fresh.

Bibendum
  • 502
  • 4
  • 7
  • 18
  • I'm having the same problem. Is any solution for this problem available besides waiting for the devolpers to fix it? –  Mar 19 '10 at 09:50
  • I had the same problem and it went away when I updated to PyDev 1.5.6. –  Apr 08 '10 at 12:09

6 Answers6

40

This happened to me. I had installed PyDev and configured it and went on my merry way. A few months later, I installed wxPython and had this same problem. An easy way to fix is in eclipse:

Window -> Preferences -> Pydev -> Interpreter - Python

Just remove the default interpreter and add a new one (it can be the same one you had before). Pydev/Eclipse searches through your Python Installation directory and adds the correct paths to your PYTHONPATH. I restarted and all was well. I noticed it added

C:\Python26\lib\site-packages\wx-2.8-msw-unicode

So you could probably just add that to the PYTHONPATH instead of going through all the above, assuming that path is where this directory is installed.

By the way, I am using:

  1. Eclipse Helios
  2. Pydev 1.6.2.2010090812
  3. Python 2.6
  4. wxPython2.8-win32-unicode-2.8.11.0-py26

But I think this should be a pretty general solution to the problem.

oob
  • 1,948
  • 3
  • 26
  • 48
  • Cheers! Note for OsX: the wxpython installer apparently puts wxpython in a different location -- after adding "/usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.6/site-packages/wx-2.8-mac-unicode" it worked for me – christianbrodbeck Oct 16 '10 at 21:15
  • Works, but on my system the problem re-appears when I start working on the project a few days later. The "from wx import wx #@UnresolvedImport" solution by 'N3nn' works best. – Cees Meijer Aug 29 '13 at 06:47
7

PyDev finds the references when you setup the interpreter in

Window -> Preferences -> Pydev -> Interpreter - Python

If wxPython was not in your site-packages directory when you first setup the interpreter, then the references to the wx objects and names will not be known to the editor lookup function. To fix this, remove the interpreter from

Window -> Preferences -> Pydev -> Interpreter - Python

and then select new. Re-add the python installation again and press apply. At this time, Pydev will import all of the site-package objects again and should populate the lookup dictionary. You'll want to restart Eclipse for changes to take place.

Davejavu
  • 71
  • 1
  • 2
3

Some of the newer versions of pydev (circa January 2010) have a hard time tracking down imported names. It's probably nothing.

If this is still occurring, report the bug to aptana appcelerator, though no doubt they already know about it.

I get this problem when working with packages I've just recently downloaded, and eventually the errors go away. My most recent problem was after downloading pygame (circa January 2010).

Edit

I've amended my answer above since people are downvoting it, and I'm assuming it's because the information is stale, or because appcelerator bought aptana. I have not used pydev with Eclipse for nearly 2 years and the situation may be different now.

  • I don't think this is a bug, although it may have been in January. See my answer below. – oob Sep 18 '10 at 23:01
  • With pydev 1.5.6.2010033101, today I still had the import error when trying to include networkx for the first time. The procedure you mentioned in your answer is what I have been using since Jan 2010 to overcome the issue. Occasionally simply restarting Eclipse has been enough (e.g. with pygame back in Jan 2010), though the interpreter remove/add works every time. –  Sep 30 '10 at 14:25
  • Also, I think I remember reading somewhere on the aptana site a recommendation to use the remove/add method you listed below as a solution. –  Sep 30 '10 at 14:27
3

Use CTRL+1 key combination on error text and add #@UndefinedVariable or #@UnresolvedImport in the end of corresponding lines with errors, it will remove these warnings temporary. See this answer: How do I fix PyDev "Undefined variable from import" errors?

Community
  • 1
  • 1
shaman.sir
  • 3,198
  • 3
  • 28
  • 36
3

Try

wx = wx

Don't ask why. This approach (that I found when trying to break the problem in smaller parts) just seems to remove the wx undefined variables problem.

Juho
  • 39
  • 1
3
#import  wx 
from    wx import wx #@UnresolvedImport

will fix.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
N3nn
  • 31
  • 1