7

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.

  • Does `cv2` appear in the list of installed packages in Project Interpreter? Have you tried adding it though that view (see e.g. https://www.jetbrains.com/pycharm/help/installing-uninstalling-and-upgrading-packages.html)? Do you have multiple interpreters on your machine (e.g. are you using `virtualenv`s)? – jonrsharpe Apr 22 '15 at 12:43
  • No cv2 doesn't appear in installed packages. It does suggest wrappers such as opencv-cython, pyopencv etc. but these are to be installed from online sources (tried installing pyopencv from there which didn't work). I can't see any option to add a package from my local machine since I have it installed there. And I am not using virtualenv or any other interpreters, I do not understand how those would benefit me – Mitali Dargani Apr 22 '15 at 12:49
  • For me, no `:$PYTHONPATH` is required. only `/usr/local/lib/python2.7/site-packages` in PYTHONPATH. – Youngjae Mar 07 '16 at 08:39

3 Answers3

0

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!

mfmain
  • 99
  • 6
0

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.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
0

Do the following steps:

  1. Download and install the OpenCV executable.

  2. Add OpenCV in the system path(%OPENCV_DIR% = /path/of/opencv/directory)

  3. Go to C:\opencv\build\python\2.7\x86 folder and copy cv2.pyd file.

  4. Go to C:\Python27\DLLs directory and paste the cv2.pyd file.

  5. Go to C:\Python27\Lib\site-packages directory and paste the cv2.pyd file.

  6. Go to PyCharm IDE and go to DefaultSettings > PythonInterpreter.

  7. Select the Python which you have installed.

  8. Install the packages numpy, matplotlib and pip in pycharm.

  9. Restart your PyCharm.

Valentino
  • 7,291
  • 6
  • 18
  • 34