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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29115126/
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 04:06:38  来源:igfitidea点击:

ImportError: No module named cv2

pythonpython-2.7opencvnumpy

提问by Alwin Hui

I've been trying to figure this error out for the past day and I've looked up error messages all over the internet and still can't figure out how to get past this error.

过去一天我一直试图找出这个错误,我已经在互联网上查找了错误消息,但仍然无法弄清楚如何克服这个错误。

I have OpenCV and cv2 setup on my desktop, but I need to program on my laptop now (for mobile reasons). Unfortunately, even though I have OpenCV downloaded, when I try to import cv2, it gives me the error message, "ImportError: No module named cv2".

我的桌面上有 OpenCV 和 cv2 设置,但我现在需要在我的笔记本电脑上编程(出于移动原因)。不幸的是,即使我下载了 OpenCV,当我尝试导入 cv2 时,它也会给我错误消息,“ImportError: No module named cv2”。

The closest I've gotten so far is "locate cv2" which gives me "/usr/lib/python2.7/dist-packages/cv2.so". I then go into the python shell and run "import sys" then "sys.path.append('/usr/lib/python2.7/dist-packages')" which then gives me a True when I ask "'/usr/lib/python2.7/dist-packages' in sys.path". However, when I then try to import cv2, it now returns a new error message of "ImportError: numpy.core.multiarray failed to import". I tried resolving this error, but I had no luck on this either.

到目前为止我得到的最接近的是“定位 cv2”,它给了我“/usr/lib/python2.7/dist-packages/cv2.so”。然后我进入 python shell 并运行“import sys”然后运行“sys.path.append('/usr/lib/python2.7/dist-packages')”当我问“'/usr /lib/python2.7/dist-packages' 在 sys.path 中”。但是,当我尝试导入 cv2 时,它现在返回一条新的错误消息“导入错误:numpy.core.multiarray 导入失败”。我尝试解决这个错误,但我也没有运气。

I've tried everything on forums and message boards online and can't figure out how to fix this. ANY help would be extremely appreciated, as I need to complete this program by the end of the week.

我已经尝试了在线论坛和留言板上的所有内容,但不知道如何解决此问题。任何帮助将不胜感激,因为我需要在本周末之前完成这个程序。

回答by Velimir Mlaker

You're missing NumPy, which can be installed one of several ways. Here's some possibilities, listed in order of (my personal) preference:

您缺少 NumPy,它可以通过多种方式之一安装。以下是一些可能性,按(我个人)偏好的顺序列出:

1) Inside a virtualenv. See https://stackoverflow.com/a/19213369/1510289on how to do this.

1) 在 vi​​rtualenv 中。有关如何执行此操作,请参阅https://stackoverflow.com/a/19213369/1510289

2) System-wide, if you have pip installed:

2)系统范围内,如果您安装了 pip:

pip install numpy

3) System-wide, using your package manager. For example on Ubuntu:

3) 系统范围内,使用您的包管理器。例如在 Ubuntu 上:

apt-get install python-numpy

or on YUM systems, like Fedora:

或者在 YUM 系统上,比如 Fedora:

yum install numpy

回答by Kingz

Another reason might be a missing OpenCV module. On my Mac OSX El Capitan [10.11.2 (15C50)], I had the exact same error with Anaconda install, and this resolved the issue:

另一个原因可能是缺少 OpenCV 模块。在我的 Mac OSX El Capitan [10.11.2 (15C50)] 上,我在安装 Anaconda 时遇到了完全相同的错误,这解决了问题:

conda install opencv

While that helped deal with:

虽然这有助于处理:

ImportError: No module named cv2

It also introduced the following issue:

它还引入了以下问题:

ImportError: numpy.core.multiarray failed to import

because somehow the numpy version got switched back to 1.7.0. So performing this, worked:

因为不知何故 numpy 版本切换回 1.7.0。所以执行这个,工作:

conda update numpy

Double check:

再检查一遍:

import numpy
print numpy.__version__
1.10.2

Now all good.

现在一切都很好。

回答by Vaibhav

I am currently working with Google VM (ubuntu 14.04). Installing opencv on python3.4 version has been quite a task. I wanted opencv to be installed for python 3.4 but everytime it was getting installed on 2.7 version.

我目前正在使用 Google VM (ubuntu 14.04)。在 python3.4 版本上安装 opencv 是一项艰巨的任务。我希望为 python 3.4 安装 opencv,但每次都安装在 2.7 版本上。

I will share the steps I followed so as to help others on it.

我将分享我遵循的步骤,以帮助其他人。

Step 1Follow all the steps as mentioned on openCv installation part till cmake. Link is given below: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

步骤 1按照 openCv 安装部分中提到的所有步骤进行操作,直到 cmake。链接如下:https: //docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

Note: Install all the 3 packages mentioned at start. That optional one too..!! And dont forget to change the python version for which you are installing.

注意:安装开始时提到的所有 3 个包。那个可选的也是.. !! 并且不要忘记更改您正在安装的python 版本。

I did

我做了

sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Follow step 2 for cmake.

按照步骤 2 进行 cmake。

Step 2For installing opencv in specific version of python (ubuntu), you have to set the default (PYTHON_DEFAULT_EXECUTABLE) with the path to where your python is installed. You can find that out by using command whereis python3.4 (or, your version). Mine was in /usr/bin/python3.4

第2步要在特定版本的python(ubuntu)中安装opencv,您必须使用安装python的路径设置默认值(PYTHON_DEFAULT_EXECUTABLE)。您可以使用命令 whereis python3.4(或您的版本)找到它。我的是在 /usr/bin/python3.4

Instead of cmake mentioned on the page, use this,

而不是页面上提到的cmake,使用这个,

cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D HAVE_opencv_python3=ON -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 ..

cmake -D CMAKE_BUILD_TYPE=发布 -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D HAVE_opencv_python3=ON -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 ..

Note: Dont forget to change your python version and path in PYTHON_DEFAULT_EXECUTABLE.

注意:不要忘记在 PYTHON_DEFAULT_EXECUTABLE 中更改您的 Python 版本和路径。

Step 3Follow the remaining steps as mentioned in the link till sudo make install

步骤 3按照链接中提到的其余步骤进行操作,直到 sudo make install

Hope it helps.

希望能帮助到你。