OpenCV 2.3.1 Python 与 Eclipse 显示 synatx 错误但仍然运行
我按照这个指南使用Eclipse在Python 2.7中设置OpenCV 2.3.1。
我还将这些库复制到我的 python 文件夹中:
这是我的Hello World 程序运行正确(创建命名窗口并显示图像),但 Eclipse 仍然显示语法错误
每个错误都表示“未定义来自导入的变量”
这是我对该项目的 python 设置:
http://i.snag.gy/KBXiB.jpg http://i.snag.gy/KfTpF.jpg
我的 PythonPath 设置不正确吗?怎样才能让Eclipse正常工作呢?
谢谢
I followed this guide to setup OpenCV 2.3.1 in Python 2.7 with Eclipse.
I also copied the libraries into my python folder:
Here is my Hello World program which runs correctly (creates a named window and displays the image) but Eclipse still shows syntax errors
every error says "Undefined variable from import"
Here are my python settings for this project:
http://i.snag.gy/KBXiB.jpg
http://i.snag.gy/KfTpF.jpg
Have I setup my PythonPath incorrectly? How can i get Eclipse to work properly?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,即使到处都有未定义的导入错误,一切都运行正常。我最终通过将“cv”添加到强制内置列表中解决了这个问题:Window > >首选项>派德夫解释器 - Python >强制内置>新的。
这就是我遇到解决方案的方式:
如何使用代码完成使用 OpenCV 进入 Eclipse
我希望这对您也有帮助。
I had the same problem, everything ran correctly even though there were undefined import errors all over the place. I eventually solved it by adding 'cv' to the list of Forced Builtins: Window > Preferences > Pydev > Interpreter - Python > Forced Builtins > New.
This is how I came across the solution:
How to use code completion into Eclipse with OpenCV
I hope that this may help you too.
编辑:仅供参考,根据此处的最佳答案 ,如果您刚刚开始(像我一样!),几乎肯定使用 cv2 接口而不是 cv2.cv 中提供的旧接口更好。该答案的作者 Abid Rahman 有 一些教程看起来不错。 (编辑结束)
我使用 Debian 的工具来安装 python-opencv 包。找不到 .../dist-packages/opencv 目录,并且 cv.py 文件仅包含:
我对 Python 相当缺乏经验,并且对 Python 访问外部库完全没有经验,所以这看起来像是某种与此相关的解决方法。显然并非如此。我按照上面 Casper 的链接找到了他使用的解决方案(这对我有用),但是当我不完全确定后果时,我不高兴使用“强制内置”。
然而,第二个评级较低的答案是我的首选解决方案。 我没有
使用
据我所知,这只是从导入链中删除了 cv.py 中间人(如果有意义的话)。我的脚本的保存/关闭/重新加载使 Eclipse 识别出 cv.LoadImageM 的定义并自动完成 OpenCV 中的其他内容。
我在这里复制这个答案是因为它对我来说似乎更清晰,当我搜索同一问题的答案时,我首先发现了这个问题。
EDIT: FYI, according to the top answer here, if you're just getting started (like me!) it's almost certainly better to use the cv2 interface instead of the older one provided in cv2.cv. The author of that answer, Abid Rahman, has some tutorials that look pretty good. (end EDIT)
I used Debian's tools to install the python-opencv package. There was no .../dist-packages/opencv directory to be found, and the cv.py file contained only:
I'm fairly inexperienced with Python and completely so with Python access to external libraries, so this looked like some sort of workaround related to that. Not so, apparently. I followed Casper's link above, and found the solution that he used (which worked for me,) but I wasn't happy using "forced builtins" when I wasn't entirely sure of the consequences.
However, the second, lower-rated answer there is my preferred solution. Instead of
I'm using
From what I can tell, this just removes the cv.py middleman from the import chain, if that makes sense. A save/close/reload of my script had Eclipse recognizing cv.LoadImageM as defined and autocompleting other things from OpenCV.
I'm reproducing that answer here because it seems cleaner to me and I found this question first when I searched for the answer to the same problem.
显示您收到的错误和代码会很有帮助。然而,我怀疑问题在于 PyDev 显示的语法错误是基于它自己对代码的解析,这比实际的 python 解释器要简单得多。如果您的代码运行,则必须定义明显未定义的变量,但 PyDev 解析器无法看到它们并将它们报告为“未定义”。
其原因是 OpenCV 没有以 PyDev 可以读取的方式显式定义其变量。不幸的是我没有一个简单的解决方案。我通常使用
from ... import ...
来处理问题,这样错误只出现一次。如果您愿意,可以编写一个包装器模块,将变量显式导入到其本地命名空间中,然后导入该模块。It would be helpful to show the error you're getting and your code. However, I suspect that the problem is that the syntax errors which PyDev shows are based on its own parsing of the code, which is much more simplistic that the actual python interpreter. If your code runs, then the apparently undefined variables must be defined, but the PyDev parser just can't see them and reports them as "undefined".
The cause of this is that OpenCV doesn't explicitly define its variables in a way which can be read by PyDev. Unfortunately I don't have an easy solution. I usually deal with the problem by using
from ... import ...
so that the error only appears once. If you want you could write a wrapper module which explicitly imports the variables into its local namespace, then import that module instead.