PyCharm can´t find reference of any OpenCV function in __init__.py

Viewed 1354

I´m using PyCharm 2021.2 Professional edition and I have installed opencv-python with:

pip install opencv-python

However, the IDE keeps giving me the following warning when I try to use cv2 package:

Cannot find reference 'resize' in 'init.py'

Here I gave the example of the resize function, but it´s happening for every function in cv2 package. Although the code runs with no errors, I can´t use the auto complete feature, which is a bit annoying. I found an answer here that might help. The guy says to use:

import cv2.cv2 as cv2

However this is not working for me. Im getting the following error:

ERROR: No matching distribution found for cv2

That´s because there is no package named cv2 inside opencv. Does anyone knows how to solve this problem? Is it a PyCharm's issue?

UPDATE
Here is the output of the command pip show opencv-python :

Name: opencv-python
Version: 4.5.3.56
Summary: Wrapper package for OpenCV python bindings.
Home-page: https://github.com/skvark/opencv-python
Author: None
Author-email: None
License: MIT
Location: z:\appdata\python\lib\site-packages
Requires: numpy
Required-by:

2 Answers

can you share the output of the command pip show opencv-python if the output is some thing like this

Name: opencv-python
Version: 4.5.1.48
Summary: Wrapper package for OpenCV python bindings.
Home-page: https://github.com/skvark/opencv-python
Author: None
Author-email: None
License: MIT
Location: c:\users\{username}\appdata\local\programs\python\python37\lib\sitepackages
Requires: numpy
Required-by:

then their is some problem with opencv's __init__.py file

can you please share the output

Thank you

by your output, it is clear that pip has properly installed cv2 on your machine

can you now share the output of

python -c "import cv2"

this is to check whether there is a problem with python or pycharam

This solution worked for me.

  • In preferences, Select Python Interpreter
  • Click the setting icon ( gear on right of box that display your Python Interpreter and select Show All
  • A list of all your configured Interpreters is show with your current interpreter already hi-lighted.
  • With your interpreter still highlighted, click the Icon that shows a folder and subfolder at the top. Tool tip should say "Show Paths for Selected Interpreter.
  • Click the + button and add the following path: /lib/python3.9/site-packages/cv2 The .../python3.9... will be different if you are using a different Python Version.
  • Click Ok until you are back to the main IDE window

Tested on Mac OS 12.4, PyCharm 2022.1

Related