'Cannot import cv2 in PyCharm
I am working on a project that requires OpenCV and I am doing it in PyCharm on a Mac. I have managed to successfully install OpenCV using Homebrew, and I am able to import cv2 when I run Python (version 2.7.6) in Terminal and I get no errors. The issue arises when I try importing it in PyCharm. I get a red underline with:
no module named cv2
I assume that PyCharm is unable to locate my cv2.so file but I have the latest PyCharm version (4.0.6) and none of the forums I've looked at are helpful for this version. How do I get PyCharm to recognise my cv2 file? I went in Project Interpreter but there is no option for importing OpenCV from my own machine. Furthermore in Edit Configurations I defined an environment variable
PYTHONPATH
and set it to
/usr/local/lib/python2.7/site-packages:$PYTHONPATH
but this didn't help either.
Any ideas?
EDIT: I set up a virtualenv to no avail and figured out how to add a path to the current framework on the new PyCharm version and it turns out the path to cv2.so has already been given yet it is still complaining.
Solution 1:[1]
I got the same situation under win7x64 with pycharm version 2016.1.1, after a quick glimpse into the stack frame, I think it is a bug!
Pycharm ipython patches import action for loading QT, matplotlib, ..., and finally sys.path lost its way!
anyway, there is a workaround, copy Lib/site-packages/cv2.pyd or cv2.so to $PYTHONROOT, problem solved!
Solution 2:[2]
Have you selected the right version of python ? or rather, when you have installed opencv with brew, this last probably has installed a new version of python that you can find in Cellar's Directory. You can see this immediately; from the main window of PyCharm select:
Configure -> Preferences -> Project Interpreter
click on Project Interpreter Combobox and be careful if there is a instance of python in Cellar's Directory, if yes, select it and you can see the cv2 in the list below.
Solution 3:[3]
Do the following steps:
Download and install the OpenCV executable.
Add OpenCV in the system
path(%OPENCV_DIR% = /path/of/opencv/directory)Go to
C:\opencv\build\python\2.7\x86folder and copycv2.pydfile.Go to
C:\Python27\DLLsdirectory and paste thecv2.pydfile.Go to
C:\Python27\Lib\site-packagesdirectory and paste thecv2.pyd file.Go to PyCharm IDE and go to DefaultSettings > PythonInterpreter.
Select the Python which you have installed.
Install the packages
numpy,matplotlibandpipin pycharm.Restart your PyCharm.
Solution 4:[4]
In a file you are importing opencv copy the following code paste it and run:
from pip._internal import main
try:
import cv2
except Exception as e:
main(["install", "opencv-python" ])
finally:
pass
**Let me know if it works, Good luck!! **
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mfmain |
| Solution 2 | Sociopath |
| Solution 3 | Valentino |
| Solution 4 | crispengari |
