Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy cannot be imported in Jupyter Notebook

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. enter image description here

Any help would be great. thank you.

like image 789
Arun Kalyanaraman Avatar asked Apr 23 '17 05:04

Arun Kalyanaraman


1 Answers

TLDR: try this

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

A bit more info:

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)

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.

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

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.

like image 151
jakevdp Avatar answered Sep 25 '22 18:09

jakevdp