20
votes

I am using OpenCV 3 and python 2.7 and coding using PyCharm. The code works fine but PyCharm does not recognize cv2 as a module. It underlines it with a red line, so it doesn't display its functions in the intellisence menu.

I tried to set an environment variable OPENCV_DIR but it didn't work

OpenCV is extracted in F:\opencv and Python is installed on C:\Python27

What is wrong ?

13
Have you copied cv2.pyd to correct python lib folder?Ruwanka Madhushan
I copied it to C:\Python27\Lib\site-packages as opencv tutorials sayAbd ElRahman Abbas

13 Answers

14
votes

here are proper instructions if you have not built opencv from source so that everyone can follow it

  • Click File menu > Settings

enter image description here

  • Search " Project Interpreter " in search bar

enter image description here

  • Click that + button to install packages or press Alt+Insert enter image description here

  • now Available packages window will open and then search for " opencv-python " and click install package button.

enter image description here

  • enjoy and do +1
10
votes

Try File->Invalidate Caches / Restart...

I can't say definitively why this works, but it may have something to do with the cached module definitions that PyCharm uses to provide code hints. In some cases they aren't updated or get corrupted. I just know that it's worked for me.

8
votes

Worked on Pycharm 4.0 version

Follow these steps:

  1. Go to File>Settings in Pycharm IDE Window

  2. Search Project Interpreter in search bar.

  3. Click on any package from the available options
  4. Package window will open from where you can install any packages.

I have installed opencv-python package to run opencv commands

3
votes

I have the same problem and I'm afraid that there isn't a solution for this at the moment. You can read more about the issue here

The problem is that OpenCV doesn't include the needed .py file to have the autocomplete which might be why PyCharm isn't picking up the package as well.

To be sure that we are on the same page, when you go File>Default Settings>Default Project>Project Interpreter and select the python 2.7 interpreter, does it list cv2 or opencv in the packages?

EDIT: I was able to get it working with anaconda instead of python. Just instaled anaconda and did everything the same as I would with python (copied cv2 where it would be in python). Hope it helps you!

3
votes

Follow the link How to install OpenCV on Windows and enable it for PyCharm without using the package manager

Steps to follow:

Install Python 2.7.10
Install Pycharm(If you have not done it already)
Download and install the OpenCV executable.
Add OpenCV in the system path(%OPENCV_DIR% = /path/of/opencv/directory)
Goto C:\opencv\build\python\2.7\x86 folder and copy cv2.pyd file.
Goto C:\Python27\DLLs directory and paste the cv2.pyd file.
Goto C:\Python27\Lib\site-packages directory and paste the cv2.pyd file.
Goto PyCharm IDE and goto DefaultSettings>PythonInterpreter.
Select the Python which you have installed on Step1.
Install the packages numpy,matplotlib and pip in pycharm.
Restart your PyCharm.
PyCharm now has OpenCV library installed and working.
3
votes

I've solved this problem. Hope this works for you also.

from cv2 import cv2

https://stackoverflow.com/a/61242587/8423105

0
votes

I follow the steps in the webapp response and after that It does not work and I decided to reinstall the pycharm IDE, this works for me. Hope it helps.

0
votes

Just install opencv python package from settings.

0
votes

Installing opencv-python package from pycharm setting worked for me.

-1
votes

You can install your existing libraries to pycharm by enabling the button "Inherit global site-packages" while creating project. If you don't have installed libraries then you can install it by going to File>Settings>Project:your project name>project interpreter and then install your required package by searching that.

-1
votes

After installing OpenCV with pip and then pip3 in terminal. It would import when writing python in terminal, but not in PyCharm. I tried the invalidation of cache mentioned above, and it worked for a min until cache was warmed up. Same result....
I fixed it by going to:

  • PyCharm Menu
  • Preferences
  • Project (proj name) -> Project Interpreter
  •          (this time instead of CV2)
  • Plus sign (to install packages)
  • searched for opencv-python
  • installed the package

I didn't even have to dot off that library, it then accepted the "import cv2"

-1
votes

Just:

pip install opencv-python

solved for me on Windows and Linux.

-2
votes

While Installing pycharm,dont select virtual environment unless u want it,If you select it ,then it will create a venv file and you need to import all the module by command prompts.Tick the existing interpreter,it will make everything easy.