简体   繁体   中英

ModuleNotFoundError: No module named 'pandas' in Jupyter Notebooks

I am using a Jupyter Notebook in VSCode for a simple data science project. I've imported pandas in the past and had no problems, only now when I try to execute the code, "ModuleNotFoundError: No module named 'pandas'" is raised in the Notebook.

I installed pandas with pip, and when I type pip install pandas into the terminal, I get "requirement already satisfied". Note: I have no problems importing pandas into a basic.py file. The error only occurs in the Jupyter Notebook. (Also, I am not using a virtual environment.)

I tried using the solution found in (Jupyter Notebook) ModuleNotFoundError: No module named 'pandas' ) by adding "C:\Users\AppData\Local\Programs\Python\Python39" to the path, but it hasn't made a difference.

    ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-9-2e52ded19b86> in <module>
----> 1 import pandas as pd
      2 df = pd.read_csv("archive\IPIP-FFM-data-8Nov2018\data-final.csv", delimiter="\t")
      3 df

ModuleNotFoundError: No module named 'pandas'

Pandas (1.2.3)

Python (3.9)

But below could help you to get to the root cause.

run pip freeze in jupiter and review if you see pandas installed. Seems like your notebook and terminal are in different environments for sure.

!pip freeze

There I was going through the same problem, but Thank God I found the solution, you are using your command line but perhaps it's not in as same location as in your Juypter notebook so, what solved my problem was installing the library in Anaconda prompt, so just do pip install the library name... Hope that answers your question;)

I had the same problem. Are you using anaconda? If so it may be an issue with the kernel/ multiple envrionments. This link https://jupyter-notebook.readthedocs.io/en/stable/troubleshooting.html tells you everything you need.

Essentially: I had multiple versions of python installed and stored in separate locations. The ~/opt/anaconda location and also a ~/.pyenv. After installing anaconda I was encountering errors in both VS code and Jupyter through Anaconda- Navigator. My kernel was using the original python location, and this was causing issues. Once I realized this it was an easy fix.

For VS code: I simply had to click the drop down and select the anaconda kernel location. Mine was already listed, each option indicates the file path in the drop down.

For Jupyter via the Anaconda- Navigator: (This is all explained in greater and more technical detail in the link above.) For Jupyter notebooks I had to go in and create a new kernel file with the correct location. I would check the location of your anaconda python file, and then use the $ jupyter kernelspec list command in your terminal to see a list of your kernels, I personally had one "python 3" you can then look at this folder and the kernel.json file within it. The kernel.json file has the location of your kernel stored, its very straight forward. If those locations do not match up you may be encountering the same problem. I just duplicated the whole "python 3" file, renamed the duplicate "Anaconda Python" and changed the kernel path. I can now see both options in the kernel drop down in Jupyter. I switched to the new kernel and everything is running fine:)

After installing Jupyter Notebook, I re-ran the anaconda install. Seemed to fix it.

I got a solution:

1 - open anaconda cmd prompt (not windows one)

2 - type in prompt:

conda info --envs

to see if you have another virtual envirovment, and if you have, it will appear a list with the NAME of env, and the current one you are marked with a *.

3 - if you see a second env, so type in prompt:

conda activate NameOfEnv

where NameOfEnv is the name you saw in the list, unmarked.

4 - now it will appear a () with the name of your new env. and you can type im prompt:

pip install pandas

5 - as a last step, you must restart you Jupyter notebook kernel, if you dont know how to do, just close and open it again.

6 - Now you can import pandas and use it:-)

All of the answers above work if you don't have pandas installed. I had similar problem, but it was jupyter related. I fixed it with:

pip3 install pandas jupyter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM