1

In Jupyter, there is an ImportError:

import numpy as np 

ImportError Traceback (most recent call last)

ipython-input-1-4ee716103900 in module()

----> 1 import numpy as np

ImportError: No mudule named numpy

But in Python, there is no error:

import numpy as np 
Saca
  • 10,355
  • 1
  • 34
  • 47
HONG ZI
  • 31
  • 2
  • 7
  • Please [don't post code as images](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) - post your code and error message as text instead. – EJoshuaS - Stand with Ukraine Feb 28 '17 at 05:16

2 Answers2

3

Usually this happens because your python is one Python environment and whatever you're running your notebook with is another. Try running import sys; sys.executable in both environments and seeing whether or not they match. If they don't, that's your problem: whatever jupyter is running in is borrowing numpy from the root environment.

Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57
  • Thanks for your answer. i run import sys;sys.executable in the python environment, it displays: /usr/bin/python. but the notebook displays: /bin/python. what can i do to solve it? – HONG ZI Feb 28 '17 at 06:43
  • I recommend installing [anaconda](https://www.continuum.io/downloads) and using their `conda` tooling to get around this. – Aleksey Bilogur Feb 28 '17 at 16:35
0

For ubunutu environment in jupyter, run:

import sys; sys.executable

This will display the python location eg

/home/paul/notepad_vm/notepad_vm/bin/python

switch to bash shell

cd /home/paul/notepad_vm/notepad_vm/bin/python

./pip install numpy

switch back to Jupyter, run:

import numpy as np
jpsmith
  • 11,023
  • 5
  • 15
  • 36
Paul
  • 1
  • 1
  • Since you are already running `pip install numpy` again (OP said already worked in Python), an alternative is to use `%pip install numpy` **from inside the Jupyter notebook cell** where you are looking to import to handle all this. This magic command was added to insure installations occur in the environment backing the kernel underlying the notebook. There is a related `%conda install ` command, too. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about the modern magic command for inside Jupyter. – Wayne Jan 20 '23 at 19:49