2
votes

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:

enter image description here

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']
1
Are you referring to the module not being in the list in the settings. Because I would guess that that is due to the fact that you self-compiled and the package manager pycharm is configured to use does not recognize it. Did you actually try to run a script in pycharm that imports cv2? - FlyingTeller
Actually the problem is, that within a simple python script, PyCharm throws the error when I try to run the program: 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' - F. Geißler
It probably doesn't work in the pycharm python console either? Try comparing the contetns of sys.path when executing from pycharm and when outside - FlyingTeller
Also, include the full error message in your question - FlyingTeller
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'] - F. Geißler

1 Answers

0
votes

Finally, I've just managed to get cv2 to work now!

The problem was, that I didn't want to use a virtual env with my Python3.

Soulution:

  1. Rebuilded the opencv libary with a slighty different command + following steps from the original guide (-D PYTHON3_EXECUTABLE=/bin/python ).

  2. Found out, that python3 (launched via OSX terminal) won't find cv2 after trying to import the module, unless I switched /usr/local/lib/python3.7/site-packages/ before launching it.

  3. Since the cv2.so file is located in this directory, which is not listed with sys.path (by weird circumstances) I had to manually append it to the path list.

There are two options which worked form me: a) Local

import sys
sys.path.insert(0, "/usr/local/lib/python3.7/site-packages/")

Right before I try to import cv2 in PyCharm

b) Global

nano ~/.bash_profile

Adding the following line:

export PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python3.7/site-packages/"

And a reload of the .bash_profile with:

. ~/.bash_profile

Thanks to @flyingteller and @eypros for the kind help.

Note: PyCharm doesn't know that cv2 exists until it compiles/launches the program. Therefor the syntax highlighting won't recognize cv2. Don't mind this line. It works for me though.