Autocomplete for OpenCV-Python in Windows not working
Asked Answered
M

3

7

I cannot get autocomplete working for OpenCV (Python) on Windows.

According to Abid's instructions here, I pasted the cv2.pyd file in the C:\Python27\Lib\site-packages.

In the Python code, I import as follows:

import cv2.cv as cv

I have also installed numpy, and it created its own folder in site-packages unlike OpenCV (which I've pasted directly into site-packages).

With this setup, the code executes without any problems, even when OpenCV methods are called.

But I have not been able to get autocomplete to work. I have tried to get it to work on Sublime Text 2 (with SublimeCodeIntel) and PyCharm. In both IDEs, autocomplete works for the numpy import, but fails for the OpenCV import.

I'm using OpenCV 2.4.6, and Python 2.7 (32 bit).

Any possible solutions?

Mystery answered 7/9, 2013 at 9:59 Comment(2)
PyCharm doesn't support is because of the known bug.Farrar
Ah. Known issues FTW. But then if I just say import cv2 in PyCharm, it does show me some autocomplete items of the library. Of course, this does not execute correctly. But then how does the autocomplete show up at all, considering that a .pyd file is like a .dll?Mystery
L
9

The reason it's not working is because you're using a .pyd file, which is essentially the same as a compiled .dll. Autocomplete works by reading the source .py files, which are plain text. Try installing the OpenCV and Intel Math Kernel Library optimized NumPy packages from Christoph Gohlke's Python Extension Packages for Windows repository, which is frequently updated and a must-use resource for anyone who does any kind of scientific Python computing on Windows. Make sure you delete the cv2.pyd and numpy directories from site-packages first. These new packages will install the .py source files needed by the autocomplete engine in Sublime Text.


EDIT

OK, so I wrote the above because it worked well for a bunch of other packages. I'm a Python 3 guy, and I never installed OpenCV from Gohlke because it only has Python 2 bindings. After reading @CrazyCoder's comment below, I booted up Win7, and indeed he's absolutely correct (and I should have realized this before) - since OpenCV is written in C/C++, the only .py file included in the Gohlke package is cv.py, whose entire contents are as follows:

from cv2.cv import *

The rest is contained in cv2.pyd and a bunch of .dlls. The full OpenCV Windows distribution from opencv.org is a 291 MB download, which expands to 3 GB, and the few .py files in there are involved in building OpenCV, and aren't any good for autocomplete purposes. So, unfortunately, I don't know if there's a solution to your problem at the moment. Just keep the docs handy, and perhaps check out OpenCV Computer Vision with Python from Packt/O'Reilly, published in April 2013.

Lie answered 7/9, 2013 at 14:16 Comment(2)
It will not help, at least not with PyCharm. Binary OpenCV installer from your link doesn't install any sources.Farrar
Bother. You're right, for some reason the OpenCV package is just binary files, but NumPy includes source as well as binary. I suppose that's because most (if not all) of OpenCV is written in C/C++, while most of NumPy is Python, although it does have some functions in C and Fortran. Let me see if I can find a workaround...Lie
R
3

I've had the same issue with PyCharm when building a custom version of OpenCV on windows. Here is what I did to fix it:

OpenCV made a folder in Python site-packages like so: opencv site-packages

So what you need to do is just add the python-3.9 folder to your interpreter.

  • File
  • Settings...
  • Python interpreter
  • Three dots icon next to your interpreter -> Show all...
  • Select your interpreter and click on the "Show paths for selected interpreter" icon
  • Add the folder inside the cv2 site-packages

Now import the cv2 module normally.

Ry answered 11/3, 2021 at 23:44 Comment(0)
T
1

The best way to solve all the problems of OPENCV-PYTHON is by uninstalling it and reinstalling it.

Even I faced the same problem.

I fixed it by:

python -m pip uninstall Opencv-python

Then I reinstalled it by using a lower version. But unfortunately, I did not know the versions of opencv; So by using a small trick you can get it by running:

python -m pip install opencv-python==

and you will get an error similar to this:

ERROR: Could not find a version that satisfies the requirement opencv-python== (from versions: 3.4.0.14, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64) ERROR: No matching distribution found for opencv-python==

Here you can see all the versions of opencv-python; choose any one (but not the latest as the error occurs due the latest version of opencv-python. install it by using:

pip install opencv-python==3.4.17.61 (You can choose your version, but this version solved the issue for me)

then enjoy your coding....

Even AUTO-COMPLETE error in opencv-python gets solved.

HAPPY CODING

Terramycin answered 11/4, 2022 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.