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

3

9

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 know 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:  
Gelman answered 5/8, 2021 at 15:49 Comment(4)
Does this answer your question? PyCharm doesn't recognise installed moduleAdlee
Unfortunately this doesn´t solve my problem. In my case, Python recognizes that the module is installed and runs the code with no error, but I can´t get auto complete feature because it can't find a reference in init.pyGelman
There is nothing wrong with your installation. If the function is implemented in C or C++ then PyCharm won't be able to resolve it. PyCharm does static analysis. If it is necessary to execute the code for the function to exist then PyCharm won't see it. The PyCharm documentation for this warning specifically says that. Just ignore the warning, or silence it.Refreshment
Does this answer your question? Python 3 Opencv set up problem: "cannot find reference 'VideoCapture' in __init__.py" on Pycharm IDEPhotoengraving
D
21

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

Draff answered 22/7, 2022 at 23:46 Comment(3)
Worked on Windows 11, PyCharm 2022.3.2. Was never a real issue, but the prompt was annoyingIodine
Amazing. Thanks! Works on mac tooPshaw
Worked for me using OpenCV 4.7 in PyCharm 2023 on Ubuntu Linux with the linux type path: /home/*USERNAME*/.local/lib/python3.10/site-packages/cv2Jibber
R
2

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

Rough answered 5/8, 2021 at 15:58 Comment(0)
G
2

If using a version of opencv-python > 4.5.5.62 then there is a bug in the pip package (see: https://github.com/opencv/opencv-python/issues/821).

This results in missing autocomplete and the Cannot find reference 'im...' in '__init__.py' in pycharm.

The current workaround to make autocomplete work and avoid the warning is to explicit install an older version of opencv.

pip install --force-reinstall --no-cache -U opencv-python==4.5.5.62

Gaiter answered 26/4, 2023 at 11:58 Comment(1)
A variant of this is what worked for me on PyCharm 2023 in Ubuntu 20.04 with Python3.8. The above solutions didn't work because my /home/*USERNAME*/.local/lib/python3.10/site-packages/ directory was empty. I am using a virtual environment interpreter, so I simply used the PyCharm's Settings ... Python interpreter window to install opencv-python 4.5.5.62 over what was there. The resulting documentation doesn't have a pretty format, but it's there.Tiling

© 2022 - 2024 — McMap. All rights reserved.