Python Jupyter 笔记本上的 cv2 导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38109270/
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
cv2 import error on Jupyter notebook
提问by Hiroyuki Nuri
I'm trying to import cv2on Jupyter notebookbut I get this error:
我正在尝试在Jupyter 笔记本上导入cv2,但出现此错误:
ImportError: No module named cv2
I am frustrated because I'm working on this simple issue for hours now. it works on Pycharm but not on Jupiter notebook. I've already installed cv2 into Python2.7's site packages, configured Jupyter's kernel to python2, browsed the documentation but I still don't get what I am missing ?
我很沮丧,因为我现在正在处理这个简单的问题几个小时。它适用于 Pycharm 但不适用于 Jupiter 笔记本。我已经将 cv2 安装到 Python2.7 的站点包中,将 Jupyter 的内核配置为 python2,浏览了文档,但我仍然不明白我缺少什么?
(I'm using windows 10 and working with microsoft cognitives api, that's why I need to import this package.)
(我正在使用 Windows 10 并使用 microsoftcognitives api,这就是我需要导入此包的原因。)
here is the code:
这是代码:
<ipython-input-1-9dee6ed62d2d> in <module>()
----> 1 import cv2
2 cv2.__version__
What should I do in order to make this work ?
我应该怎么做才能使这项工作?
回答by plfrick
Is your python path looking in the right place? Check where python is looking for the module. Within the notebook try:
你的 python 路径找对地方了吗?检查 python 在哪里寻找模块。在笔记本内尝试:
import os
os.sys.path
Is the cv2
module located in any of those directories? If not your path is looking in the wrong place. If it is overlooking the install location, append it to your python path. You can follow the instructions here.
该cv2
模块是否位于任何这些目录中?如果不是,您的路径在错误的位置。如果它忽略了安装位置,请将其附加到您的 python 路径。您可以按照此处的说明进行操作。
回答by Aleksandar
I didn't have the openCV installation in my Python3 kernel, so I installed it by activating the specific environment and running this in the command prompt:
我的 Python3 内核中没有安装 openCV,所以我通过激活特定环境并在命令提示符下运行它来安装它:
pip install opencv-python
How to find and activate my environment?
如何找到并激活我的环境?
To list all of Your conda
environments, run this command:
要列出您的所有conda
环境,请运行以下命令:
conda info --envs
You will get something like this:
你会得到这样的东西:
ipykernel_py2 D:\Anaconda\envs\ipykernel_py2
root D:\Anaconda
After that, activate the environment that is complaining for the missing cv2
and run the pip install opencv-python
command.
之后,激活抱怨丢失的环境cv2
并运行pip install opencv-python
命令。
How to activate an environment?
如何激活环境?
Just run the command:
只需运行命令:
activate env_name
where env_name
is the wanted environment (for example, You could type activate ipykernel_py2
if You wanted to access the first of the two environments listed above).
env_name
想要的环境在哪里(例如,activate ipykernel_py2
如果您想访问上面列出的两个环境中的第一个,则可以键入)。
Note:If You are on Linux, You need to type source activate env_name
.
注意:如果您使用的是 Linux,则需要输入source activate env_name
.
回答by sandeep dubey
It is because of opencv library. Try running this command in anaconda prompt:
这是因为opencv库。尝试在 anaconda 提示符下运行此命令:
conda install -c conda-forge opencv
回答by binmosa
To make this clear for those who are having the same issue:
为了让有同样问题的人清楚这一点:
By default: Anaconda (jupyter notebook) has its own version of Python & packages once it has been installed on your PC.
默认情况下:Anaconda(jupyter 笔记本)一旦安装在您的 PC 上,就会拥有自己的 Python 版本和软件包。
If you have Python x.x installed on your PC, and you installed OpenCV or -whatever packages- using the package manager of this python version, it does NOT mean your jupyter notebook will get access to these python packages you installed earlier. They are not living in the same folder.
如果您的 PC 上安装了 Python xx,并且您使用此 python 版本的包管理器安装了 OpenCV 或任何包,这并不意味着您的 jupyter notebook 将可以访问您之前安装的这些 python 包。他们不在同一个文件夹中。
To illustrate this, open your windows CMD and write :
为了说明这一点,请打开您的 Windows CMD 并编写:
`python`
then write:
然后写:
import os
os.path
you will get the path of your python. in my case (C:\Python35)
你会得到你的python的路径。在我的情况下 (C:\Python35)
Now open the Anaconda Prompt and write the same commands again:
现在打开 Anaconda Prompt 并再次编写相同的命令:
`python`
then write:
然后写:
import os
os.path
you will get the anaconda's python path. In my case (C:\Users\MY_NAME\Anaconda3).
您将获得 anaconda 的 python 路径。就我而言(C:\Users\MY_NAME\Anaconda3)。
As you can see, there are two different paths of python, so make sure that your first step in diagnosing such error (No module named x) is to ask yourself whether you installed the package in the right place or not!
如您所见,python有两种不同的路径,因此请确保诊断此类错误(No module named x)的第一步是问自己是否将软件包安装在正确的位置!
N.B: within Anaconda itself you can create environments, each environment may have different packages installed in it, so you also have to make sure you're in the right environment and it is the active one.
注意:在 Anaconda 本身中,您可以创建环境,每个环境中可能安装了不同的包,因此您还必须确保您处于正确的环境中并且它是活动的。
回答by Ag371
pip install opencv-python
This solved the error for me in MacOS.
这解决了我在 MacOS 中的错误。
回答by Sander Vanden Hautte
I had this issue in my Jupyter Notebook after I had "installed" the opencv package, using Anaconda Navigator, on my base (root) environment.
在我的基本(根)环境中使用 Anaconda Navigator “安装”了 opencv 包后,我在我的 Jupyter Notebook 中遇到了这个问题。
However, after "installing" the package and its dependencies, Anaconda Navigator showed a reminder popup to update to the next Anaconda Navigator version. I ignored this at first, but couldn't use the opencv package in my Jupyter Notebook.
但是,在“安装”软件包及其依赖项后,Anaconda Navigator 会显示一个提醒弹出窗口以更新到下一个 Anaconda Navigator 版本。起初我忽略了这一点,但无法在我的 Jupyter Notebook 中使用 opencv 包。
After I didupdate Anaconda Navigator to the newer version, the opencv package install worked fine.
我之后做的较新版本更新蟒蛇导航,OpenCV的软件包安装工作的罚款。