gnarois
gnarois

Reputation: 339

(Jupyter Notebook) ModuleNotFoundError: No module named 'pandas'

In my terminal, I ran:

pip install pandas
pip3 install pandas

Installation seemed to go well. When I write some code in a file and execute it in my terminal (prompting 'python filename.py' or 'python3 filename.py'), the pandas library can be imported and used without a problem. However, when using Jupyter Lab and Jupyter Notebook, and I get this error when trying to import pandas:

ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-38d4b0363d82> in <module>
----> 1 import pandas


ModuleNotFoundError: No module named 'pandas'

It seems like Jupyter Notebook does not recognize this library. Very confused as of why and what I should do. FYI reinstalling anaconda did not help, and I am using 'pip' and 'pip3' to install libraries.

Upvotes: 2

Views: 57347

Answers (2)

user2227902
user2227902

Reputation: 31

I had a similar problem. Your best bet is to install your packages direct from Jupyter notebook, then you can be sure that the packages are being installed into the local python instance.

! pip install --user <package>

The ! tells the notebook to execute the cell as a shell command.

Upvotes: 3

Sam Mason
Sam Mason

Reputation: 16184

you've got at least 3 versions of Python installed (the system version, a copy of 3.7 and 3.8). you need to figure out which is which, i.e. what you've done to your system!

to know which version of Python is being run you can use something like (from your shell/command prompt, not in Python):

which python3

see here for an explanation and alternatives. this tells you where some version of Python is, but you can also ask for pip3 and conda, jupyter, etc, to see where those have ended up, and to make sure you're running the right one. note that this involves your "shell's path" which you can customise so it picks the right one

next you need tools to figure out the equivalent "within Python". Python libraries aren't independent programs/executables (i.e. what $PATH determines) so this is a seperate set of options

to display where Python looks for code you can do this (inside Python):

import sys
print(sys.path)

see here for more info about what's going on here

note that what happens when you "open jupyter notebook by clicking on the icon with Anaconda Navigator" is a bit more difficult to debug. sys.executable might be useful to figure out what's going on

if you know xkcd, we're sort of in this state

Upvotes: 0

Related Questions