3

I'm facing weird issue in my Jupyter-notebook.

In my first cell:

import sys
!{sys.executable} -m pip install numpy
!{sys.executable} -m pip install Pillow

In the second cell:

import numpy as np
from PIL import Image

But it says : ModuleNotFoundError: No module named 'numpy'

ModuleNotFoundError: No module named 'numpy'

I have used this command to install Jupyter notebook :

sudo apt install python3-notebook jupyter jupyter-core python-ipykernel 

Additional information :

pip --version
pip 20.2.2 from /home/maifee/.local/lib/python3.7/site-packages/pip (python 3.7)
python --version
Python 3.7.5
asked Sep 5, 2020 at 17:26
2
  • 3
    Gosh, did you start the jupyter server as root? Otherwise its ! pip install --user. Commented Sep 5, 2020 at 17:38
  • @Suuuehgi , thanks it worked... Commented Sep 6, 2020 at 2:43

6 Answers 6

7

Thanks to @suuuehgi. When Jupyter Notebook isn't opened as root:

import sys
!{sys.executable} -m pip install --user numpy
answered Sep 11, 2020 at 8:03
Sign up to request clarification or add additional context in comments.

Comments

5

I've had occasional weird install issues with Jupyter Notebooks as well when I'm running a particular virtual environment. Generally, installing with pip directly in the notebook in this form:

!pip install numpy

fixes it. Let me know how it goes.

answered Sep 5, 2020 at 18:31

2 Comments

@Lynn_Leifker, this was already outdated information when you posted it. Best practice at that point was already %pip install numpy for when installing directly in the notebook using pip. The modern magic commands were added to insure the install occurs in the correct environment. See here for more about the magic install commands added a few years ago. The exclamation point alone doesn't do that. See the ...
<continued> second paragraph here for more about why the exclamation point is not as good and can lead to issues.
2

Restarting the kernal from the Jupyter Notebook solved this issue for me

answered Feb 6, 2023 at 21:13

Comments

0

I had a similar issue. Turns out I renamed an upstream path. And I hadn't deactivated my conda env first. When I deactivated the env.

conda deactivate

Then when I activated it again, everything was as it should have been.

conda activate sample

Now I am seeing other issues with jupyter themes... but its not impacting my numpy code. So, at least I fixed the "ModuleNotFoundError: No module named 'numpy'" error

answered Dec 31, 2021 at 2:47

Comments

0

I have the same problem. My numpy is installed, I am using the same folder as usual. If I try 'conda deactivate', I get the message: ValueError: The python kernel does not appear to be a conda environment. Please use %pip install instead. I added a print of the 'pip install numpy' result and the 'Module not found error' after

Comments

0

Here is a solution which worked for me:

lib_path="c:\\users\\user\\python_39\\lib\\site-packages\\"
MODULE_NAME = "module_to_import"
MODULE_PATH = lib_path+MODULE_NAME+"\\__init__.py"
import importlib
import sys
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module 
spec.loader.exec_module(module)
import module_to_import
answered Apr 5, 2022 at 10:34

1 Comment

There is simpler approach for you @ stackoverflow.com/a/15109660/1686903

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.