在 Ubuntu 上为 Python 安装 OpenCV,得到 ImportError: No module named cv2.cv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25215102/
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
Installing OpenCV for Python on Ubuntu, getting ImportError: No module named cv2.cv
提问by robm
I have an Ubuntu 14.04 system, on which I want to install OpenCV and use it with Python 2.x.
我有一个 Ubuntu 14.04 系统,我想在其上安装 OpenCV 并将其与 Python 2.x 一起使用。
I installed OpenCV using the instructions here: https://help.ubuntu.com/community/OpenCV
我使用此处的说明安装了 OpenCV:https: //help.ubuntu.com/community/OpenCV
The install seemed to run properly, no errors, the script ended with output
安装似乎运行正常,没有错误,脚本以输出结束
OpenCV 2.4.9 ready to be used
When I try to run the sample Python script, I get the following:
当我尝试运行示例 Python 脚本时,我得到以下信息:
$ python opencv.py
Traceback (most recent call last):
File "opencv.py", line 1, in <module>
from cv2.cv import *
ImportError: No module named cv2.cv
I suspect I know why, I just don't know how to fix it. OpenCV installed to the current directory I was in when I ran the install script, it's a subdirectory of my home folder.
我怀疑我知道为什么,我只是不知道如何解决它。OpenCV 安装到我运行安装脚本时所在的当前目录,它是我的主文件夹的子目录。
Others who get this import error after install seem to be having a path issue, and have luck adding this to their code:
安装后出现此导入错误的其他人似乎遇到了路径问题,并且很幸运将其添加到他们的代码中:
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
or updating their PYTHONPATH with that same directory. I tried adding that code, it doesn't make a difference. I don't see any files in the "site-packages" directory. Should I have done the install in that directory? I imagine the installation instructions would have spelled that out. I suspect that my problem has to do with Python not finding the OpenCV install, but I'm not sure how to proceed.
或使用相同的目录更新他们的 PYTHONPATH。我尝试添加该代码,它没有任何区别。我在“site-packages”目录中没有看到任何文件。我应该在那个目录下安装吗?我想安装说明会说明这一点。我怀疑我的问题与 Python 找不到 OpenCV 安装有关,但我不确定如何继续。
Please help me get a usable install of OpenCV as simply as possible.
请帮助我尽可能简单地安装 OpenCV。
回答by Juan David
Use pip:
使用点:
$ pip install SomePackage
[...]
Successfully installed SomePackage
And when you add a path to PYTHONPATH with sys, PYTHONPATH it's always restarted to default values when you close your Python shell. Check this thread:
当您使用 sys, PYTHONPATH 添加到 PYTHONPATH 的路径时,它总是在您关闭 Python shell 时重新启动为默认值。检查这个线程:
First add openCV to your path (Quick guide):
首先将 openCV 添加到您的路径(快速指南):
after that, install the non-python packages pyopencv depends on:
之后,安装 pyopencv 依赖的非 python 包:
sudo apt-get build-dep python-opencv
finally, use pip:
最后,使用pip:
pip install pyopencv
Also, you can check this tutorial to install openCV in ubuntu 14.04 LTS
此外,您可以查看本教程以在 ubuntu 14.04 LTS 中安装 openCV
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
回答by robm
I found a solution in the guide here:
我在这里的指南中找到了解决方案:
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
I resorted to compiling and installing from source. The process was very smooth, had I known, I would have started with that instead of trying to find a more simple way to install. Hopefully this information is helpful to someone.
我求助于从源代码编译和安装。过程非常顺利,如果我知道,我会从这个开始,而不是试图找到更简单的安装方法。希望这些信息对某人有帮助。
回答by nilsmagnus
If you want as simple as possible, install from the repository:
如果您想要尽可能简单,请从存储库安装:
sudo apt-get install python-opencv libopencv-dev python-numpy python-dev
回答by ozguronur
I think you don't have the python-opencvpackage.
我想你没有这个python-opencv包裹。
I had the exact same problem and
我遇到了完全相同的问题
sudo apt-get install python-opencv
solved the issue for me.
为我解决了这个问题。
you can install opencv from the following link https://www.learnopencv.com/install-opencv3-on-ubuntu/It works for me . apt-get install doesnt contain many packages of opencv
您可以从以下链接https://www.learnopencv.com/install-opencv3-on-ubuntu/安装 opencv 它对我有用。apt-get install 不包含许多 opencv 包
回答by DeDenker
回答by Martin Vseticka
My environment:
我的环境:
- Ubuntu 15.10
- Python 3.5
- Ubuntu 15.10
- 蟒蛇 3.5
Since none of the previous answers worked for me, I downloaded OpenCV 3.0 from http://opencv.org/downloads.htmland followed the installation manual. I used the following cmakecommand:
由于之前的答案都不适合我,我从http://opencv.org/downloads.html下载了 OpenCV 3.0并按照安装手册进行操作。我使用了以下cmake命令:
$ ~/Programs/opencv-3.0.0$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3.5 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages ..
Each step of the tutorial is important. Particularly, don't forget to call sudo make install.
教程的每一步都很重要。特别是,不要忘记调用sudo make install.
回答by ashensw
I also had this issue. Tried different things. But finally
我也有这个问题。尝试了不同的东西。但最后
conda install opencv
worked for me.
为我工作。
回答by u5635006
Find where the cv2.sois, for example /usr/local/lib/python2.7/dist-packages, then add this into your ~/.bashrcby doing:
cv2.so例如/usr/local/lib/python2.7/dist-packages,找到它的位置,然后~/.bashrc通过执行以下操作将其添加到您的:
sudo gedit ~/.bashrc
and add
并添加
export PYTHONPATH=/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
In the last line
在最后一行
And then remember to open another terminal, this can be work, and I have solve my problem. Hope it can help you.
然后记得打开另一个终端,这可以工作,我已经解决了我的问题。希望它可以帮助你。
回答by user6033883
Create a symbolic link to OpenCV. Eg:
创建指向 OpenCV 的符号链接。例如:
cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
ln -s /usr/local/lib/python2.7/dist-packages/cv.py cv.py
回答by Harun ERGUL
If you really sure that you installed cv2 but it gives no module error. There is a solution for this. Probably you have cv2.sofile in your directory
如果你真的确定你安装了 cv2 但它没有给出模块错误。有一个解决方案。可能你cv2.so的目录中有文件
/usr/local/lib/python2.7/site-packages/cv2.so
move this cv2.sofile to
将此cv2.so文件移动到
/usr/lib/python2.7/site-packages
copy the file into site-packages directory
将文件复制到 site-packages 目录中

