Python 使用 OpenCV 时找不到模块 cv2

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

Cannot find module cv2 when using OpenCV

pythonopencvraspberry-pi

提问by user2971844

I have installed OpenCVon the Occidentalis operating system (a variant of Raspbian) on a Raspberry Pi, using jayrambhia's script found here. It installed version 2.4.5.

我已经安装的OpenCV的悬铃木操作系统(Raspbian的变体)上树莓派上,使用jayrambhia的剧本找到这里。它安装了 2.4.5 版。

When I try import cv2in a Python program, I get the following message:

当我尝试import cv2Python 程序时,我收到以下消息:

pi@raspberrypi~$ python cam.py
Traceback (most recent call last)
File "cam.py", line 1, in <module>
    import cv2
ImportError: No module named cv2

The file cv2.sois stored in /usr/local/lib/python2.7/site-packages/...

该文件cv2.so存储在/usr/local/lib/python2.7/site-packages/...

There are also folders in /usr/local/libcalled python3.2 and python2.6, which could be a problem but I'm not sure.

也有/usr/local/lib名为 python3.2 和 python2.6 的文件夹,这可能是一个问题,但我不确定。

Is this a path error perhaps? Any help is appreciated, I am new to Linux.

这可能是路径错误吗?任何帮助表示赞赏,我是 Linux 新手。

回答by lanpa

Try to add the following line in ~/.bashrc

尝试添加以下行 ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

回答by Midhun

This happens when python cannot refer to your default site-packages folder where you have kept the required python files or libraries

当python无法引用您保存所需python文件或库的默认站点包文件夹时会发生这种情况

Add these lines in the code:

在代码中添加这些行:

import sys

sys.path.append('/usr/local/lib/python2.7/site-packages')

or before running the python command in bash move to /usr/local/lib/python2.7/site-packagesdirectory. This is a work around if you don't want to add any thing to the code.

或者在 bash 中运行 python 命令之前移动到/usr/local/lib/python2.7/site-packages目录。如果您不想在代码中添加任何内容,这是一种解决方法。

回答by tarun kumar Sharma

I solved my issue using the following command :

我使用以下命令解决了我的问题:

conda install opencv

回答by Harun ERGUL

I have come accross same as this problem i installed cv2 by

我遇到了与我安装 cv2 相同的问题

pip install cv2

However when i import cv2 module it displayed no module named cv2 error.
Then i searched and find cv2.pydfiles in my computer and i copy and paste to site-packages directory

但是,当我导入 cv2 模块时,它没有显示名为 cv2 错误的模块。
然后我cv2.pyd在我的电脑中搜索并找到文件,然后复制并粘贴到 site-packages 目录

C:\Python27\Lib\site-packages

then i closed and reopened existing application, it worked.

然后我关闭并重新打开现有的应用程序,它起作用了。

EDITI will tell how to install cv2correctly.

编辑我会告诉如何cv2正确安装。

1.Firstly install numpy on your computer by

1.首先在您的计算机上安装numpy

pip install numpy


2.Download opencvfrom internet (almost 266 mb).
I download opencv-2.4.12.exefor python 2.7. Then install this opencv-2.4.12.exe file.
I extracted to C:\Users\harun\Downloadsto this folder.
After installation go look for cv2.pyinto the folders.
For me


2.从网上下载opencv(将近 266 mb)。
我下载opencv-2.4.12.exe了 python 2.7。然后安装这个 opencv-2.4.12.exe 文件。
我解压C:\Users\harun\Downloads到这个文件夹。
安装后去寻找cv2.py文件夹。
为了我

C:\Users\harun\Downloads\opencv\build\python.7\x64

in this folder take thecv2.pydand copy it in to the

在这个文件夹中,把cv2.pyd它复制到

C:\Python27\Lib\site-packages

now you can able to use cv2in you python scripts.

现在你可以cv2在你的python脚本中使用了。

回答by Tes3awy

Try this out:

试试这个:

sudo ldconfig

sudo nano /etc/ld.so.conf.d/opencv.conf

and add this following line in the opencv.confnot in the command window

并在opencv.conf不在命令窗口中添加以下行

/usr/local/lib

Then:

然后:

sudo ldconfig

sudo nano /etc/bash.bashrc

and add this two lines in the bash.bashrcnot in the command window

并在bash.bashrc不在命令窗口中添加这两行

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig       
export PKG_CONFIG_PATH

at last reboot your Pi sudo reboot now

最后重启你的 Pi sudo reboot now

and try import cv2

并尝试 import cv2

回答by Eamonn Kenny

I had the same problem, just couldn't figure it out with opencv2 and opencv3 installed into /opt/opencv and opencv3 respectively. Turned out that bloody anaconda install of opencv in my home directory was first on path and mangled opencv. Removed it and started using /opt/opencv3/lib as defined in /etc/ld.so.conf.d/opencv.conf. Worked perfectly first go. Do you have anaconda installed? Could be the issue.

我遇到了同样的问题,只是无法将 opencv2 和 opencv3 分别安装到 /opt/opencv 和 opencv3 中。原来,我的主目录中的 opencv 的血腥 anaconda 安装首先在路径上并损坏了 opencv。删除它并开始使用 /etc/ld.so.conf.d/opencv.conf 中定义的 /opt/opencv3/lib。工作完美第一次去。你有安装anaconda吗?可能是问题。

回答by Rika

First do run these commands inside Terminal/CMD:

首先在终端/CMD 中运行这些命令:

conda update anaconda-navigator  
conda update navigator-updater  

then the issue for the instruction below will be resolved

那么下面指令的问题将得到解决

for windows if you have anaconda installed, you can simply do

对于 Windows,如果你安装了 anaconda,你可以简单地做

pip install opencv-python

or

或者

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

if you are on linux you can do :

如果您使用的是 linux,则可以执行以下操作:

pip install opencv-python

or

或者

conda install opencv 

Link1Link2

链接1链路2

For python3.5+ check these links : Link3, Link4

对于 python3.5+ 检查这些链接:Link3Link4

回答by Fatmajk

None of the above answers worked for me. I was going crazy until I found this solution below!

以上答案都不适合我。我快疯了,直到我在下面找到了这个解决方案!

Simply run:

只需运行:

sudo apt install python-opencv

回答by Shiva Manhar

If want to install opencv in virtual environment. Run command in terminal for getting virtual environment list.

如果想在虚拟环境中安装 opencv。在终端中运行命令以获取虚拟环境列表。

conda env list

or jupyter notebook command is

或 jupyter notebook 命令是

!conda env list

Then update your anaconda.

然后更新你的蟒蛇。

conda update anaconda-navigator
conda update navigator-updater

Install opencv in your selected environment path.

在您选择的环境路径中安装 opencv。

conda install -c ['environment path'] opencv

Juypter notebook

Juypter 笔记本

!conda install --yes --prefix ['environment path'] opencv

回答by Anoop Kumar

Python3.x User

Python3.x 用户

sudo pip3 install opencv-python