2

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.

gnarois
  • 199
  • 1
  • 4
  • 11
  • 1
    Does this answer your question? [numpy & pandas 'ModuleNotFoundError' in Jupyter notebook (Python 3)](https://stackoverflow.com/questions/43872617/numpy-pandas-modulenotfounderror-in-jupyter-notebook-python-3) – Sam Mason Nov 07 '19 at 01:24
  • In the link provided by Sam Mason do tell us , what you are unable to understand . Then we might be able to help. – nitin3685 Nov 07 '19 at 01:29
  • @SamMason Unfortunately, no... I tried what is proposed in the main answer. In my case, pandas does show when I prompt 'pip list'. So I guess I have to 'activate the environment' though not sure what that means. When in the terminal I prompt 'activate 'ENV_NAME'', I get an error message: activate: command not found. – gnarois Nov 07 '19 at 01:31
  • I presume you need [`conda activate ENV_NAME`](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html), but I don't use Conda or Windows so can't check either! – Sam Mason Nov 07 '19 at 01:34
  • @NicolasFraisse sorry, not sure why I assumed you were on Windows! how are you starting this jupyter notebook? you need to make sure it's using the same Python environment as you installed your pandas package into. also note that `pip` and `pip3` might be different, and it's worth figuring out which one is the right one so you don't end up scattering packages all over your system unnecessarily – Sam Mason Nov 07 '19 at 02:00
  • @SamMason no problem! So, how could I know which environment my jupypter notebook is using? I open jupyter notebook by clicking on the icon with Anaconda Navigator. But I can also open it by prompting `jupyter notebook` but I still get the same error. – gnarois Nov 07 '19 at 02:24
  • @SamMason Sorry for the double response. If that helps, when I type `import sys` and then `sys.version` on my terminal, I get `'3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) \n[Clang 6.0 (clang-600.0.57)]'` whereas in Jupyter Notebook, running the same lines lead to the following version: `'3.7.5 (default, Nov 1 2019, 02:16:32) \n[Clang 11.0.0 (clang-1100.0.33.8)]'` – gnarois Nov 07 '19 at 02:28
  • @NicolasFraisse I don't know what state you've got your system into, so can't help much. I've answered with some of the tools (and pointers to more info) I'd use to get myself out. I'd recommend reading about them and maybe following through some tutorials on how the command line works. there's quite a lot going on, but most of it is there for good reasons – Sam Mason Nov 07 '19 at 09:23

2 Answers2

3

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.

function
  • 1,298
  • 1
  • 14
  • 41
  • This does not work for me. This one does: https://stackoverflow.com/questions/42321784/jupyter-modulenotfounderror-no-module-named-matplotlib – Yu Shen Aug 12 '22 at 21:23
0

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

Sam Mason
  • 15,216
  • 1
  • 41
  • 60