Abd ElRahman Abbas
Abd ElRahman Abbas

Reputation: 363

PyCharm does not recognize cv2 as a module

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 IntelliSense 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?

Upvotes: 35

Views: 124614

Answers (15)

efaisalzia
efaisalzia

Reputation: 45

Reason of Error is Because cv2 and opencv are not python packages. they are availabale as a part opencv-python packages.

terminal command to install

pip install opencv-python

or go to Pycharm Files> Settings or go to Pycharm Files > Settings

then look for Python Interpretr or Project Interpreter

Project>Python Interpretr or Project Interpreter

Click + sign and look for opencv-python and click install Click + sign and look for opencv-python and click install

Upvotes: 1

Josh Larson
Josh Larson

Reputation: 835

What ended up working for me was uninstalling the package from pip and installing it from my package manager (apt):

python3 -m pip uninstall opencv-python
sudo apt install python3-opencv

Upvotes: 0

user889030
user889030

Reputation: 4754

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

Upvotes: 40

kimDragonHyeon
kimDragonHyeon

Reputation: 434

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

from cv2 import cv2

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

Upvotes: 14

geekly
geekly

Reputation: 678

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.

Upvotes: 18

DANISH REZA
DANISH REZA

Reputation: 1

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.

Upvotes: -2

Hareesh Kesireddy
Hareesh Kesireddy

Reputation: 19

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

Upvotes: 1

Max
Max

Reputation: 795

Just:

pip install opencv-python

solved for me on Windows and Linux.

Upvotes: -2

Navod B ak
Navod B ak

Reputation: 11

Just install opencv python package from settings.

Upvotes: 0

atreyHazelHispanic
atreyHazelHispanic

Reputation: 109

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"

Upvotes: -1

Talha Farooq
Talha Farooq

Reputation: 54

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.

Upvotes: -1

Guru
Guru

Reputation: 105

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

Upvotes: 8

Denis Romasanta
Denis Romasanta

Reputation: 11

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.

Upvotes: 0

webapp
webapp

Reputation: 708

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.

Upvotes: 5

ILA
ILA

Reputation: 83

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!

Upvotes: 3

Related Questions