无法在Jupyter Notebook中导入Scipy [英] Scipy cannot be imported in Jupyter Notebook

查看:768
本文介绍了无法在Jupyter Notebook中导入Scipy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jupyter笔记本中使用scipy,它说我已经安装了它,但是当我尝试导入它时,它给了我以下错误.

I'm trying to use scipy in a jupyter notebook and it says I have it installed, but when I try to import it, it gives me the following error.

任何帮助都会很棒.谢谢.

Any help would be great. thank you.

推荐答案

TLDR:试试这个

import sys
!{sys.executable} -m pip install scipy

更多信息:

Jupyter笔记本能够使用多个内核,这些内核本质上是指向笔记本使用的Python(或其他语言)可执行文件的指针.在Python内核中,您可以通过键入

Jupyter notebooks are able to work with multiple kernels, which are essentially pointers to the Python (or other language) executable that the notebook uses. In a Python kernel, you can figure out which one is being used by typing

import sys
print(sys.executable)

在笔记本中运行bash命令(如!pip install scipy)时,会使用启动笔记本时处于活动状态的bash环境,不一定与您正在使用的Python内核相关联 .这意味着它可能会在其他Python位置安装scipy.您可以通过运行!which python来弄清楚shell指向哪个Python.如果不匹配,则!pip install不会安装在正确的位置.

When you run a bash command in the notebook, like !pip install scipy, that uses the bash environment that was active when you launched the notebook which is not necessarily associated with the Python kernel you are using. That means that it may be installing scipy in a different Python location. You can figure out which Python your shell points to by running !which python. If this doesn't match, then !pip install will not be installing in the right place.

您可以通过明确告诉bash提示要使用哪个Python/pip来解决此问题.例如,这应该可以解决问题:

You can fix this by explicitly telling the bash prompt which Python/pip you want to use. For example, this should do the trick:

import sys
!{sys.executable} -m pip install scipy

这将运行与您的可执行文件关联的pip版本,并随即安装scipy.有关幕后发生情况的更多详细信息,请查看此答案.

This runs the pip version associated with your executable, and installs scipy with that. For some more details on what's happening behind the scenes, check out this answer.

这篇关于无法在Jupyter Notebook中导入Scipy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆