简体   繁体   中英

Pycharm cannot recognize cv2 but still usable

I recently compiled the OpenCV 4.4.0 with CUDA and Python3 bindings following this instruction. I install it to a virtualenv in Anaconda, and it works in Anaconda Prompt. But as I want to use it in Pycharm , the code import cv2 is underlined with the hint (just underlined, not run yet):

No module named cv2

Even though I could import errorless and use it anyway. And then there's no "hint info" as my cursor was on the code eg cv2.imread() (normally there would be hints about input name and format etc.). So the problem should just be that Pycharm couldn't recognize it.

Could anyone enlighten me a little? Thanks!

界面

Emmm I've solved it myself in a stupid way.

Since the problem is we only have the .pyd binding file, we need to make it a python package with reference files in it, like what we'd get from a normal cv2 module from pip install . So I just:

  • create a new virtual env
  • pip install opencv-python=4.4.0.42 (or another version you desire)
  • copy all files except for the .pyd file to the target virtual env. In my case it's located in [PathToAnacondaEnvs]\\MoVis_gpu\\Lib\\site-packages\\cv2\\ , so that __init__.py will make cv2 as a legitimate python package, and other files would add reference for it.

Not elegant, but it works :)

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