dexter2406
dexter2406

Reputation: 461

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 e.g. 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!

interface

Upvotes: 3

Views: 180

Answers (1)

dexter2406
dexter2406

Reputation: 461

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 :)

Upvotes: 1

Related Questions