1

I'm trying to install OpenCV into my python environment (Windows), and I'm almost all of the way there, but still having some issues with autocomplete and Pycharm itself importing the library. I've been through countless other related threads, but it seems like most of them are either outdated, for prebuilt versions, or unanswered.

I'm using Anaconda and have several environments, and unfortunately installing it through pip install opencv-contrib-python doesn't include everything I need. So, I've built it from source, and the library itself seem to be working fine. The build process installed some things into ./Anaconda3/envs/cv/Lib/site-packages/cv2/: __init__.py, some config py files, and .../cv2/python-3.8/cv2.cp38-win_amd64.pyd. I'm not sure if it did anything else.

But here's where I'm at:

  • In a separate environment, a pip install opencv-contrib-python both runs and has autocomplete working
  • In this environment, OpenCV actually runs just fine, but the autocomplete doesn't work and Pycharm complains about everything, eg: Cannot find reference 'imread' in '__init__.py'
  • Invalidate Caches / Restart doesn't help
  • Removing and re-adding the environment doesn't help
  • Deleting the user preferences folder for Pycharm doesn't help
  • Rebuilding/Installing OpenCV doesn't help
  • File->Settings->Project->Project Interpreter is set correctly
  • Run->Edit Configuration->Python Interpreter is set correctly

So my question is: how does Pycharm get or generate that autocomplete information? It looks like the pyd file is just a dll in disguise, and looking through the other environment's site-packages/cv2 folder, I don't see anything interesting. I've read that __init__.py has something to do with it, but again the pip version doesn't contain anything (except there's a from .cv2 import *, but I'm not sure how that factors in). The .whl file you can download is a zip that only contains the same as what 'pip install' gets.

Where does the autocomplete information get stored? Maybe there's some way to copy it from one environment to another? It would get me almost all the way there, which at this point would be good enough I think. Maybe I need to rebuild it with another flag I missed?

AMC
  • 2,642
  • 7
  • 13
  • 35
arietman
  • 51
  • 4
  • _I'm using Anaconda and have several environments, and unfortunately installing it through pip install opencv-contrib-python doesn't include everything I need._ If you're using Conda, then why install the library through pip? _So, I've built it from source_ All it should take is `conda install opencv`. – AMC Apr 19 '20 at 00:27

2 Answers2

4

Got it finally! Figures that would happen just after posting the question...

Turns out .../envs/cv/site-packages/cv2/python-3.8/cv2.cp38-win_amd64.pyd needed to be copied to .../envs/cv/DLLs/. Then PyCharm did it's magic and is now all good.

arietman
  • 51
  • 4
  • same problem here.. but on Ubuntu .. any suggestion? – rok May 12 '20 at 16:26
  • In my case, after copying the `.pyd` file into the DLLs folder. Need to delete the original `cv2.cp38-win_amd64.pyd` under `site-packages/cv2/python-3.8/` – Lê Quang Duy Jun 14 '21 at 04:42
4

Alternatively add the directory containing the .pyd file to the interpreter paths.

I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.

I solved this way:

  1. Select project interpreter
  2. Click on the settings button next to it and then clicking on the Show paths for selected interpreter
  3. adding the directory containing the cv2 library (in my case in the Conda Python library path - e.g. miniconda3/lib/python3.7/site-packages/cv2/python-3.7). In general check the site-packages/cv2/python-X.X directory)
Marco
  • 2,389
  • 2
  • 16
  • 19