7

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:

http://i.snag.gy/J9RrC.jpg

Here is my Hello World program which runs correctly (creates a named window and displays the image) but Eclipse still shows syntax errors

Eclipse showing 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

josh
  • 1,231
  • 1
  • 12
  • 28

3 Answers3

5

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.

Community
  • 1
  • 1
hjweide
  • 11,893
  • 9
  • 45
  • 49
2

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:

from cv2.cv import *

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

import cv

I'm using

import cv2.cv as cv

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.

Community
  • 1
  • 1
wil
  • 579
  • 1
  • 4
  • 14
0

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.

aquavitae
  • 17,414
  • 11
  • 63
  • 106