7

I've searched through other questions but have not found anything that has helped (most just suggest you do install pandas with conda or pip). In my jupyter notebook I'm trying to import pandas (import pandas as pd) but I'm getting the following error:

ModuleNotFoundError: No module named 'pandas'

Some pertinent information:

  • I'm using python3
  • I've installed pandas using conda install pandas
  • My conda environment has pandas installed correctly. After activating the environment, I type python into the terminal and from there I can successfully import pandas and use it appropriately. This leads me to believe that it is an issue with my jupyter notebook.
CGul
  • 147
  • 1
  • 3
  • 11

6 Answers6

4

You can try: which conda and which python to see the exact location where conda and python was installed and which was launched.

And try using the absolute path of conda to launch jupyter.

For example, /opt/conda/bin/jupyter notebook

kingbase
  • 1,268
  • 14
  • 23
  • The absolute path is literally the only thing that made it work for me! – ivanacorovic Mar 07 '19 at 02:44
  • I had the same issue and the problem was I had different versions of Jupyter installed. You can see this with `which -a jupyter`. If you have two different paths and the first one is not in the `conda` directory, that could be the issue. I didn't want to use the absolute path so I solved this by changing the order of elements in my $PATH. – roborg Jan 28 '20 at 14:45
4

For me on Mac OSX, this was solved by installing jupyter using pip, rather than homebrew

pip3 install jupyter

Then the jupyter notebook has access to all python packages, including pandas.

Another option would be to install pandas inside of the jupyter notebook:

!pip install pandas
philshem
  • 24,761
  • 8
  • 61
  • 127
1

Try this for python3

sudo pip3 install pandas

1

Its seems using homebrew installs for packages dependancies of home brew formulas are not handled by home brew well. Mostly path issues as installs are in different locations vs pip3, I also had tried installing pandas thru nb with !pip3, but I got errors that is was already satisfied meaning it was already installed just not importing. I uninstalled homebrew jupyterlab and used pip3 instead and all worked proper as a work around.

0

The default kernel in jupyter notebook points the python that is different to the python used inside the terminal. You could check using which python

So the packages installed by conda lives in different place compared to the python that is used by the jupyter notebook at default.

To fix the issue, both needs to be same.

For that create a new kernel using ipykernel. syntax: python -m ipykernel install --user --name custom_name --display-name "Python (custom_name)"

After that, check the custom kernel and the path of the python used. jupyter kernel list --json

Finally, Restart the jupyter notebook. And change the kernel to the new custom_kernel.

0

The environment is broken or your IDE needs an update. Proof by example, this time with exifread as the module:

(scrape) C:\Users\Admin>python
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import exifread
>>>

Thus, exifread can be imported in the "scrape" environment.

Yet, in VSCode, when you choose a conda environment, this "scrape" env does not show up like the other environments bd/ml/dl, it is the last item in the list and does not have the ('scrape': conda) at the end:

enter image description here

If I choose this "scrape" environment, and I had chosen the "bd" environment before, it seems to work as always, it gets chosen in the status:

enter image description here

But strangely, if I then run an installation in that "scrape" env (taking the hint from the other answer, I get:

enter image description here

Or in writing:

from pathlib import Path
!pip install exifread
import exifread

Throws:

Requirement already satisfied: exifread in c:\users\admin\anaconda3\envs\bd\lib\site-packages (3.0.0)

Thus, Jupiter Notebook did not switch to the "scrape" env, it is still in the former "bd" env.

And if I drop that installation command !pip install exifread and run it, it does not find the module "exifread", as was the same with "pandas" in the question:

ModuleNotFoundError: No module named 'exifread'

If I choose one of the other environments, it works. Thus, check another environment or rebuild the environment.

While I thought this would fix it, I got an update just today and everything looked new, the "scrape" env was listed in the dropdown like the other envs:

enter image description here

And when chosen, the env now pops up in the upper right, and it runs through:

enter image description here

questionto42
  • 7,175
  • 4
  • 57
  • 90