eclipse 在pydev中使用wxPython时导入的未定义变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2143549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined variable from import when using wxPython in pydev
提问by Bibendum
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"
我刚刚下载了 wxPython,并且正在运行这里的一些示例程序。但是,在使用 wx.* 中的变量的每一行上,我都会收到一个“来自导入错误的未定义变量”
For example, the following program generates five errors on lines 1,4,8, and two on line 5:
例如,以下程序在第 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()
The program, however, compiles and runs perfectly. I haven't made any significant modifications to pydev or eclipse, and the wxPython install is fresh.
但是,该程序可以完美地编译和运行。我没有对 pydev 或 eclipse 进行任何重大修改,并且 wxPython 安装是全新的。
采纳答案by Davejavu
Some of the newer versions of pydev (circa January 2010)have a hard time tracking down imported names. It's probably nothing.
一些较新版本的 pydev (大约 2010 年 1 月)很难追踪导入的名称。应该没什么吧
If this is stilloccurring, report the bug to aptanaappcelerator, though no doubt they already know about it.
如果这仍然发生,请向aptana appcelerator报告该错误,尽管他们肯定已经知道了。
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).
我在处理我最近下载的软件包时遇到了这个问题,最终错误消失了。我最近的问题是在下载 pygame (大约 2010 年 1 月) 之后。
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.
我已经修改了上面的答案,因为人们对其进行了否决,我假设这是因为信息陈旧,或者因为 appcelerator 购买了 aptana。我已经将近 2 年没有在 Eclipse 中使用 pydev,现在情况可能有所不同。
回答by oob
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:
这发生在我身上。我已经安装了 PyDev 并对其进行了配置,然后继续我的快乐之路。几个月后,我安装了 wxPython 并遇到了同样的问题。一个简单的修复方法是在 eclipse 中:
Window -> Preferences -> Pydev -> Interpreter - Python
窗口 -> 首选项 -> Pydev -> 解释器 - 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
只需删除默认解释器并添加一个新解释器(它可以与您之前使用的相同)。Pydev/Eclipse 搜索您的 Python 安装目录并将正确的路径添加到您的 PYTHONPATH。我重新启动,一切都很好。我注意到它添加了
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.
因此,您可能只需将其添加到 PYTHONPATH 中,而不是执行上述所有操作,假设该路径是安装此目录的位置。
By the way, I am using:
顺便说一下,我正在使用:
- Eclipse Helios
- Pydev 1.6.2.2010090812
- Python 2.6
- wxPython2.8-win32-unicode-2.8.11.0-py26
- 日蚀太阳神
- Pydev 1.6.2.2010090812
- 蟒蛇 2.6
- wxPython2.8-win32-unicode-2.8.11.0-py26
But I think this should be a pretty general solution to the problem.
但我认为这应该是该问题的一个非常通用的解决方案。
回答by Davejavu
PyDev finds the references when you setup the interpreter in
PyDev 在您设置解释器时找到引用
Window -> Preferences -> Pydev -> Interpreter - Python
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
如果第一次设置解释器时 wxPython 不在您的站点包目录中,那么编辑器查找功能将不知道对 wx 对象和名称的引用。要解决此问题,请从
Window -> Preferences -> Pydev -> Interpreter - Python
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.
然后选择新建。再次重新添加python安装并按应用。此时,Pydev 将再次导入所有站点包对象,并应填充查找字典。您需要重新启动 Eclipse 以进行更改。
回答by shaman.sir
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?
CTRL+1
在错误文本上使用组合键并在相应的错误行末尾添加#@UndefinedVariable
或#@UnresolvedImport
,它将临时删除这些警告。请参阅此答案:如何修复 PyDev“来自导入的未定义变量”错误?
回答by Juho
Try
尝试
wx = wx
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.
不要问为什么。这种方法(我在尝试将问题分解为较小的部分时发现的)似乎只是消除了 wx 未定义变量问题。
回答by N3nn
#import wx
from wx import wx #@UnresolvedImport
will fix.
将修复。