OpenCV built from source: Pycharm doesn't get autocomplete information

六月ゝ 毕业季﹏ 提交于 2020-05-15 20:59:08

问题


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?


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/61190224/opencv-built-from-source-pycharm-doesnt-get-autocomplete-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!