CMake之后在PyCharm中找不到模块cv2(OpenCV)

时间:2018-07-12 07:03:04

标签: python python-3.x opencv pycharm

我刚在OSX上遇到一个奇怪的问题,试图在我的PyCharm IDE中使用cv2。 首先,我用brew在本地安装了Python3的新副本(准确地说是3.7),然后选择该实例作为我在PyCharm中的项目解释器。

通过终端验证正确的路径:

$ which python3
/usr/local/bin/python3

由于pip3 install opencv-python无法安装该模块,因此我使用了自编译method from Adrian Rosebrock,该命令在命令执行后立即成功编译了

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 ..

然后我继续将 cv2.cpython-35m-darwin.so 文件重命名为 cv2.so 重新启动后,我直接在终端中检查了cv2模块的版本,看起来很有希望。

$ 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'

问题:在PyCharm中,即使使用选定的python3解释器,IDE也无法识别cv2模块。

PyCharm屏幕截图:

enter image description here

PyCharm错误消息:

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

有人知道我想念什么吗?

评论更新:

PyCharm控制台的

sys.path输出:

['/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'] 

与OSX终端相比:

$ 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 个答案:

答案 0 :(得分:0)

最后,我刚刚设法使cv2正常工作!

问题是,我不想在我的Python3中使用虚拟环境。

解决方案:

  1. 使用稍微不同的命令+遵循原始指南(-D PYTHON3_EXECUTABLE = / bin / python)的步骤重建了opencv库。

  2. 发现,尝试导入模块后,python3(通过OSX终端启动)将找不到cv2,除非我切换了 /usr/local/lib/python3.7/site-packages/ ,然后再启动它。

  3. 由于cv2.so文件位于此目录中,因此未在sys.path中列出(由于奇怪的情况),我不得不手动将其附加到路径列表中。

有两种方法对我有效: a)本地

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

就在我尝试在PyCharm中导入cv2之前

b)全局

nano ~/.bash_profile

添加以下行:

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

然后使用以下命令重新加载.bash_profile:

. ~/.bash_profile

感谢@flyingteller@eypros的帮助。

注意:PyCharm在编译/启动程序之前不知道cv2存在。因此,语法高亮将无法识别cv2。不要介意这条线。不过对我有用。