I've just faced a weird problem on OSX, trying to use cv2 with my PyCharm IDE. Firstly I installed a fresh copy of Python3 (3.7 to be exact) locally with brew and selected this instance as my project interpreter in PyCharm.
Validating the correct path via terminal:
$ which python3
/usr/local/bin/python3
Since pip3 install opencv-python
failed to install the module, I went with the self-compiling method from Adrian Rosebrock, which I compiled successfully right after the command
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON3_LIBRARY=/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin/libpython3.7.dylib \
-D PYTHON3_INCLUDE_DIR=/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/include/python3.7m-darwin/ \
-D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=ON ..
Then I went on renaming the cv2.cpython-35m-darwin.so file to cv2.so After a reboot I checked the cv2 module version directly in my terminal which looked promising.
$ python3
Python 3.7.0a2 (v3.7.0a2:f7ac4fe52a, Oct 16 2017, 21:11:18)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0-pre'
Problem: In PyCharm, even with the selected python3 interpreter, the IDE won't recognize the cv2 module.
PyCharm Screenshot:
PyCharm error message:
Traceback (most recent call last):
File "/Users/felix/PycharmProjects/MMW/Seminar5-2(@Python3)/test.py", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
Process finished with exit code 1
Does anyone know, what I am missing?
Update from comment:
sys.path output from the PyCharm console:
['/Applications/PyCharm Edu.app/Contents/helpers/pydev', '/Applications/PyCharm Edu.app/Contents/helpers/pydev', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages', '/Users/felix/PycharmProjects/MMW']
In comparison from the OSX terminal:
$ python3 Python 3.7.0a2 (v3.7.0a2:f7ac4fe52a, Oct 16 2017, 21:11:18) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
sys.path
when executing from pycharm and when outside – FlyingTeller