简体   繁体   中英

Autocomplete for OpenCV-Python in Windows not working

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?

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 .dll s. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM