在 pydev 中使用 wxPython 时导入未定义的变量
我刚刚下载了 wxPython,并运行了此处的一些示例程序。但是,在使用 wx.* 中的变量的每一行上,我都会收到“导入错误中未定义的变量”。
例如,以下程序在第 1、4、8 行上生成五个错误,在第 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()
该程序,然而,编译和运行完美。我没有对 pydev 或 eclipse 进行任何重大修改,并且 wxPython 安装是全新的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您设置解释器时,PyDev 会找到引用
如果当您第一次设置解释器时 wxPython 不在您的 site-packages 目录中,那么编辑器查找功能将无法识别对 wx 对象和名称的引用。要解决此问题,请从以下位置删除解释器
然后选择新建。再次重新添加 python 安装并按应用。此时,Pydev 将再次导入所有站点包对象,并应填充查找字典。您需要重新启动 Eclipse 才能使更改生效。
PyDev finds the references when you setup the interpreter in
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
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.
一些较新版本的 pydev(大约 2010 年 1 月) 很难追踪导入的名称。大概没什么吧
如果这种情况仍然发生,请将错误报告给
aptanaappcelerator,尽管毫无疑问他们已经知道了。我在使用最近下载的软件包时遇到了这个问题,最终错误消失了。我最近的问题是在下载 pygame (大约 2010 年 1 月) 后出现的。
编辑
我修改了上面的答案,因为人们对它投了反对票,我假设这是因为信息过时,或者因为 appcelerator 购买了 aptana。我已经将近 2 年没有在 Eclipse 中使用 pydev 了,现在情况可能有所不同。
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
aptanaappcelerator, 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.
在错误文本上使用
CTRL+1
组合键,并在有错误的相应行末尾添加#@UndefineVariable
或#@UnresolvedImport
,它将暂时删除这些警告。请参阅此答案:如何修复PyDev“导入时未定义变量”错误?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?尝试
wx = wx
不要问为什么。这种方法(我在尝试将问题分解为较小的部分时发现)似乎只是消除了 wx 未定义变量问题。
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.
会修复的。
will fix.
这发生在我身上。我已经安装了 PyDev 并配置了它,然后继续我的快乐之路。几个月后,我安装了wxPython并遇到了同样的问题。一个简单的修复方法是在 Eclipse 中:
Window ->首选项->派德夫 ->解释器 - Python
只需删除默认的解释器并添加一个新的解释器(可以与之前的解释器相同)。 Pydev/Eclipse 搜索您的 Python 安装目录并将正确的路径添加到您的 PYTHONPATH。我重新启动,一切都很好。我注意到它添加了
所以您可能只需将其添加到 PYTHONPATH 而不是遍历上述所有内容,假设该路径是安装此目录的位置。
顺便说一句,我正在使用:
但我认为这应该是问题的一个非常通用的解决方案。
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
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:
But I think this should be a pretty general solution to the problem.