Python 错误:没有名为 cv2 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23040428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:10:37  来源:igfitidea点击:

Error: No module named cv2

pythonopencv

提问by skm

I have already found few questionsat SO but i am unable to solve this problem using the answers there.

我已经在 SO 上发现了几个问题,但我无法使用那里的答案解决这个问题。

I am new to python. I am having python in Ubuntu 12.04. In my /usr/local/lib, there are two python folders python 2.7and python 3.2. python 2.7contains dist-packages and site-packages while python 3.2contains only dist-packages.

我是python的新手。我在 Ubuntu 12.04 中有 python。在 my 中/usr/local/lib,有两个 python 文件夹python 2.7python 3.2. python 2.7包含 dist-packages 和 site-packages 而python 3.2只包含 dist-packages。

I am trying to run a very simple opencv example with the following code:

我正在尝试使用以下代码运行一个非常简单的 opencv 示例:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image.JPG')

kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

Error:No module named cv2

错误:没有名为 cv2 的模块

采纳答案by Abid Rahman K

NB : This answer is a short compilation of comments above. For more details, please refer to the comments below question.

注意:这个答案是上面评论的简短汇编。有关更多详细信息,请参阅问题下方的评论。

Background: OP is using SPE Stani's python editor. OP has installed OpenCV /opt/ros/hydro/lib/python2.7/dist-packageswhich is not detected by the above mentioned editor. Adding this path to PYTHONPATH doesn't solve the issue.

背景:OP 正在使用SPE Stani's python editor. OP 已安装 OpenCV /opt/ros/hydro/lib/python2.7/dist-packages,但上述编辑器未检测到该OpenCV 。将此路径添加到 PYTHONPATH 并不能解决问题。

Solution (any one of the below):

解决方案(以下任一项):

  1. Add this path to sys.pathand put it in every file.
  1. 将此路径添加到sys.path并将其放入每个文件中。

import sys sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages')

import sys sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages')

  1. Copy cv2.sofile to any directory in the sys.path.
  1. cv2.so文件复制到sys.path.

回答by v4vjk

I am able to resolve this problem of not loading cv2 dll by following below steps on my Win 64 machine.

通过在我的 Win 64 机器上执行以下步骤,我能够解决不加载 cv2 dll 的问题。

  1. Download the Unofficial Windows Binaries for Python Extension Packages from gohlkesite.
  2. In my case it is opencv_python-3.2.0-cp35-cp35m-win_amd64.whl downloaded it to some folder e.g. c:\py\lib
  3. Install this .whl using below command

    pip install c:\py\lib\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl

  4. Restart the kernel and error gone.
  1. gohlke站点下载 Python 扩展包的非官方 Windows 二进制文件。
  2. 就我而言,它是 opencv_python-3.2.0-cp35-cp35m-win_amd64.whl 将其下载到某个文件夹,例如 c:\py\lib
  3. 使用以下命令安装此 .whl

    pip 安装 c:\py\lib\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl

  4. 重新启动内核,错误消失。