Anaconda:即使安装了 opencv,也无法导入 cv2(如何为 python3 安装 opencv3)

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

Anaconda: cannot import cv2 even though opencv is installed (how to install opencv3 for python3)

pythonpython-3.xopencvanacondaopencv3.0

提问by Linda

I have anaconda (version: conda 4.2.9, python3) installed and am trying to do import cv2when I get the following error:

我安装了 anaconda(版本:conda 4.2.9,python3),import cv2当我收到以下错误时正在尝试执行:

ImportError: No module named 'cv2'

ImportError: No module named 'cv2'

With conda search cv2I get this:

随着conda search cv2我得到这个:

opencv 2.4.2 np15py26_0 defaults
2.4.2 np15py27_0 defaults
2.4.2 np16py26_0 defaults
2.4.2 np16py27_0 defaults
2.4.2 np17py26_0 defaults
2.4.2 np17py27_0 defaults
2.4.2 np15py26_1 defaults
2.4.2 np15py27_1 defaults
2.4.2 np16py26_1 defaults
2.4.2 np16py27_1 defaults
2.4.2 np17py26_1 defaults
2.4.2 np17py27_1 defaults
2.4.6 np16py26_0 defaults
2.4.6 np16py27_0 defaults
2.4.6 np17py26_0 defaults
2.4.6 np17py27_0 defaults
2.4.6 np18py26_0 defaults
2.4.6 np18py27_0 defaults
2.4.9 np18py27_0 defaults
2.4.10 np19py26_0 defaults
2.4.10 np19py27_0 defaults
2.4.10 np110py27_1 defaults
2.4.10 np19py26_1 defaults
2.4.10 np19py27_1 defaults

的OpenCV 2.4.2 np15py26_0默认
2.4.2 np15py27_0默认
2.4.2 np16py26_0默认
2.4.2 np16py27_0默认
2.4.2 np17py26_0默认
2.4.2 np17py27_0默认
2.4.2 np15py26_1默认
2.4.2 np15py27_1默认
2.4.2 np16py26_1默认
2.4.2 np16py27_1默认值
2.4.2 np17py26_1默认
2.4.2 np17py27_1默认
2.4.6 np16py26_0默认
2.4.6 np16py27_0默认
2.4.6 np17py26_0默认
2.4.6 np17py27_0默认
2.4.6 np18py26_0默认
2.4.6 np18py27_0默认
2.4.9 np18py27_0默认
2.4.10 np19py26_0默认
2.4.10 np19py27_0 默认值
2.4.10 np110py27_1 默认值
2.4.10 np19py26_1 默认值
2.4.10 np19py27_1 默认值

What do I need to do to be able to import the cv2 module? Thanks!

我需要做什么才能导入 cv2 模块?谢谢!

PS: I am using Ubuntu 16.04

PS:我使用的是 Ubuntu 16.04

回答by Linda

opencv is not compatible with python 3. I had to install opencv3 for python 3. The marked answer in how could we install opencv on anaconda?explains how to install opencv(3) for anaconda:

opencv 与 python 3 不兼容。我必须为 python 3 安装 opencv3。我们如何在 anaconda 上安装 opencv 中的标记答案解释了如何为 anaconda 安装 opencv(3):

Run the following command:

conda install -c https://conda.binstar.org/menpo opencv

I realized that opencv3 is also available now, run the following command:

conda install -c https://conda.binstar.org/menpo opencv3

Edit on Aug 18, 2016:You may like to add the "menpo" channel permanently by:

conda config --add channels menpo

And then opencv can be installed by:

conda install opencv (or opencv3)

Edit on Aug 14, 2017:"clinicalgraphics" channel provides relatively newer vtk version for very recent python3

conda install -c clinicalgraphics vtk

Edit on April 16, 2020 (based on @AMC's comment):OpenCV can be installed through conda-forge(details see here)

conda install -c conda-forge opencv

运行以下命令:

conda install -c https://conda.binstar.org/menpo opencv

我意识到 opencv3 现在也可用了,运行以下命令:

conda install -c https://conda.binstar.org/menpo opencv3

2016 年 8 月 18 日编辑:您可能希望通过以下方式永久添加“menpo”频道:

conda config --add channels menpo

然后可以通过以下方式安装opencv:

conda install opencv (or opencv3)

2017 年 8 月 14 日编辑:临床图形”频道为最近的 python3 提供了相对较新的 vtk 版本

conda install -c clinicalgraphics vtk

2020年4月16日编辑(基于@AMC的评论):OpenCV可以通过安装conda-forge(详情见这里

conda install -c conda-forge opencv

回答by Hong

You can try

你可以试试

conda install -c menpo opencv=3

回答by Paulo Ricardo

Can be too late, but i'm going to help. I had the same problem, i've created my conda env and installed everything i'll need, but when i've tryed to import cv2, i've received a message 'no module named cv2'. I've tryed install again and i've done the Linda's tips and haven't worked. So, i've seen that my conda env haven't had the cv2 installed. If it happened you, you just copy your cv2 folder to "miniconda'X'/envs/yourEnv/lib/python'X.X'/site-packages/" that will work. At least have worked to me

可能太晚了,但我会帮忙的。我遇到了同样的问题,我已经创建了我的 conda env 并安装了我需要的所有东西,但是当我尝试导入 cv2 时,我收到了一条消息“没有名为 cv2 的模块”。我再次尝试安装,我已经完成了 Linda 的提示,但没有成功。所以,我已经看到我的 conda env 没有安装 cv2。如果发生这种情况,您只需将 cv2 文件夹复制到“miniconda'X'/envs/yourEnv/lib/python'X.X'/site-packages/”即可。至少对我有用

回答by Pronab Roy

The file may be not installed properly, please see the bellow the instructions.

该文件可能没有正确安装,请参阅下面的说明。

Run the following commands

运行以下命令

Sudo apt-get update
Sudo apt-get upgrade

then

然后

pip install opencv-python

check-in your jupyter notebook

签入您的jupyter 笔记本

import cv2
print cv2.__version__

This may solve the issue.

这可能会解决问题。

回答by Ama Mo

1- use this code in anaconda prompt : conda update conda -c conda-canary

1- 在 anaconda 提示中使用此代码: conda update conda -c conda-canary

2- then simply don't type this code : conda install -c conda-forge opencv, but use : Pip install opencv-python.

2-然后不要输入此代码 : conda install -c conda-forge opencv,而是使用 : Pip install opencv-python

it worked with me after few days of trials

经过几天的试验,它对我有用

reference: Krishna Ojha @https://www.youtube.com/watch?v=T2ykss_4Af0read the comments below the video

参考:Krishna Ojha @ https://www.youtube.com/watch?v=T2ykss_4Af0阅读视频下方的评论