Skip to content
Advertisement

Jupyter-notebook failed to import python packages

I was trying to use numpy in Jupyter Notebook in a Python3 virtual environment, but the encountered an error.

In terminal, I did:

$ python3 -m venv py3env
$ source py3env/bin/activate
(py3env)$ jupyter notebook

And on the Jupyter page, I created a new notebook and executed the followings

!pip install numpy
import numpy as np

And the resulting output is this:

Requirement already satisfied: numpy in /Users/MyName/py3env/lib/python3.9/site-packages (1.20.2)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-d83b6b4f94f9> in <module>
      1 get_ipython().system('pip install numpy')
----> 2 import numpy as np

ModuleNotFoundError: No module named 'numpy'

The package is already installed but still cannot be found…?

Is there a way to fix this issue?

Advertisement

Answer

You may be using pip from a different python installation.

To install with the same python executable as your jupyter kernel, in one cell run:

import sys

Then in another cell run:

!{sys.executable} -m pip install numpy
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement