简体   繁体   English

无法在 OSX 的 python 中导入 cv2

[英]Cannot import cv2 in python in OSX

I have installed OpenCV 3.1 in my Mac, cv2 is also installed through pip install cv2 .我在我的 Mac 上安装了 OpenCV 3.1,cv2 也通过pip install cv2

vinllen@ $ pip install cv2
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Requirement already satisfied (use --upgrade to upgrade): cv2 in /usr/local/lib/python2.7/site-packages

But it looks like cv2 and cv cannot be used:但看起来cv2cv不能使用:

Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv

I have tried almost all the solutions list online, but cannot work.我已经尝试了几乎所有在线解决方案列表,但无法正常工作。

You can install by您可以通过安装

pip install opencv-python

I do not know what pip install cv2 actually installs... but is surely not OpenCV.我不知道pip install cv2实际安装了什么......但肯定不是OpenCV。 pip install cv2 actually installs this , which are some blog distribution utilities , not sure what it is, but it is not OpenCV. pip install cv2实际上安装了这个,这是一些博客分发实用程序,不确定它是什么,但它不是OpenCV。


To properly install OpenCV, check any of the links @udit043 added in the comment, or refer to any of the tutorials bellow:要正确安装 OpenCV,请检查评论中添加的任何链接 @udit043,或参考以下任何教程:

Find here a tutorial on how to install OpenCV on OS X: http://www.pyimagesearch.com/2015/06/15/install-opencv-3-0-and-python-2-7-on-osx/在这里找到关于如何在 OS X 上安装 OpenCV 的教程:http: //www.pyimagesearch.com/2015/06/15/install-opencv-3-0-and-python-2-7-on-osx/

You need to actually compile OpenCV from source and activate python bindings, which takes a while.您需要实际从源代码编译 OpenCV 并激活 python 绑定,这需要一段时间。

Another option is to use brew to get OpenCV, but doesn't neccesarilly get you the last version nor a fully optimized one:另一种选择是使用brew获取 OpenCV,但不一定会为您提供最新版本或完全优化的版本:

https://web.archive.org/web/20171218032016/http://www.mobileway.net/2015/02/14/install-opencv-for-python-on-mac-os-x/ https://web.archive.org/web/20171218032016/http://www.mobileway.net/2015/02/14/install-opencv-for-python-on-mac-os-x/

I used conda install opencv and it installed fine for me.我使用了conda install opencv ,它对我来说安装得很好。

You might want to try this if you are using Anaconda.如果您使用的是 Anaconda,您可能想试试这个。

For me installation using conda didn't worked.对我来说,使用conda安装没有用。 While installing it using pip worked:使用 pip 安装它时有效:

pip install opencv-python

os: macos Catalina操作系统:macos Catalina

Make sure that numpy, other dependency is installed prior installing OpenCV确保在安装 OpenCV 之前安装了 numpy 和其他依赖项
Also if you installed using PIP then check the installed packages using此外,如果您使用 PIP 安装,则使用检查已安装的软件包

pip freeze

You can install by您可以通过安装

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

I had the same problem;我有同样的问题; here's what worked for me: conda install -c conda-forge nb_conda这对我有用: conda install -c conda-forge nb_conda

If you haven't already, do the following to get conda up and running on OS X (taken from docs ):如果您还没有,请执行以下操作以在 OS X 上启动并运行 conda(取自docs ):

  1. Download Miniconda下载Miniconda
  2. Download Anaconda下载蟒蛇
  3. Locate to the director that contains the Miniconda file and run bash Miniconda3-latest-MacOSX-x86_64.sh in Terminal找到包含 Miniconda 文件的目录并在终端中运行bash Miniconda3-latest-MacOSX-x86_64.sh
  4. Follow the prompts to install Anaconda按照提示安装Anaconda
  5. Run conda install -c conda-forge nb_conda运行conda install -c conda-forge nb_conda

You could also try conda install -c conda-forge opencv and conda install -c conda-forge/label/broken opencv if step 5 doesn't work, as someone recommended when I had the same problem.如果第 5 步不起作用,您也可以尝试conda install -c conda-forge opencvconda install -c conda-forge/label/broken opencv ,正如我遇到同样问题时有人建议的那样。 Hope this helps!希望这可以帮助!

pip is renamed to pip3 so please use pip 已重命名为 pip3,因此请使用

pip3 install opencv-python pip3 安装 opencv-python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM