Reputation: 363
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
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
then look for Python Interpretr or Project Interpreter
Click + sign and look for opencv-python and click install
Upvotes: 1
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
Reputation: 4754
here are proper instructions if you have not built opencv from source so that everyone can follow it
now Available packages window will open and then search for " opencv-python " and click install package button.
Upvotes: 40
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
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
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
Reputation: 19
Installing opencv-python package from pycharm setting worked for me.
Upvotes: 1
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:
I didn't even have to dot off that library, it then accepted the "import cv2"
Upvotes: -1
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
Reputation: 105
Worked on Pycharm 4.0 version
Follow these steps:
Go to File>Settings in Pycharm IDE Window
Search Project Interpreter in search bar.
I have installed opencv-python package to run opencv commands
Upvotes: 8
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
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
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