12

I have been successfully using pandas.read_csv since long but suddenly it starts giving the error while I try to read a csv file

df = pd.read_csv('file.csv', encoding='utf-8')

The error is

AttributeError: module 'pandas' has no attribute 'read_csv'

I have tried to upgrade pandas but does not work. I tried to search and got this answer but when I search csv.py file in my pandas I didn't find any. So i tried to hover over the pandas.read_csv method which takes me to parsers.py file. But in that file there is no specific method named read_csv but it directed to another parser funtion like this

# parser.py (built-in file in pandas) file has this implementation
read_csv = _make_parser_function('read_csv', sep=',')
read_csv = Appender(_read_csv_doc)(read_csv)

I don't understand how should it start working again ? Any suggestions

Community
  • 1
  • 1
muazfaiz
  • 4,611
  • 14
  • 50
  • 88
  • 6
    Do you have a file named `pandas.py` in that folder? – ayhan Apr 29 '17 at 13:04
  • I just noticed that I don't have `pandas.py` but test_`pandas.py`. similarly I don't have `csv.py` but I have `test_to_csv.py`. Now what does that mean ? – muazfaiz Apr 29 '17 at 13:08
  • 2
    When you use statements like `import something` Python first looks at the folder you are running that script. If there is a file named `something.py`, it imports that. So when you think you are importing pandas, you might be importing your own script. And like in the question you linked, similar problems may occur with other name conflicts (like `csv.py`). – ayhan Apr 29 '17 at 13:11

10 Answers10

8

I had the same problem when trying to run the following code in Jupyter/ipython.

import pandas as pd
df = pd.read_csv("weather_data.csv")
df

I realized I had a file named pandas.py. In fact, had two others named pandas1.py and pandas2.py as well. I changed them all and then it worked perfectly:) Lesson learned.

Drew
  • 81
  • 3
  • 1
    Having the same issue. how did you locate these pandas.py, pandas1.py files? Where should i search & rename them? Kindly help – Vikrant Arora Aug 16 '20 at 15:54
5

So I am writing an answer myself. I just noticed that I created a file random.py in my project which was creating a conflict with random.py in pandas package. Renaming my current file to something else worked for me :)

muazfaiz
  • 4,611
  • 14
  • 50
  • 88
0

I faced the same problem and the solution that worked for me is as below.

Initially I installed the pandas and numpy with a regular user account. It installed library but there were few conflicts. So I uninstalled the libraries using pip uninstall package then installed them back as sudo account using sudo -H pip install package.

I hope it helps other people facing similar issue.

Simon.Hermit
  • 89
  • 2
  • 9
0

You literally just need to make sure that you have no ".py" files that have names of the same names of the packages. Like pandas.py, numpy.py etc..

0

Try print(pd)

Make sure you are getting this kind of output

<module 'pandas' from 'C:\\Users\\adarsh\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\pandas\\__init__.py'>

Otherwise, there may be another python file named pandas in your current working directory

For more click here

András Aszódi
  • 8,948
  • 5
  • 48
  • 51
Adarsh Raj
  • 31
  • 4
0

I also got the same problem but when I made a separate directory for the python file problem was solved. Make sure there is no other python file in your directory.

0

It is either the file named pandas.py or csv.py in the same directory or a name similar to one of the pandas files(which are many) in the same directory. I had a file named tokenize.py which it did not like. You can try checking in the pandas directory (search results in pandas) whether there are similar file names.

0

your project contains pandas.py possibly. rename it and try again.. would work.

ady
  • 1
-1

Put your csv file in the directory or folder where your python project files are . This solution worked for me.

-1

Make sure that, the library imported and the filename you are executing or the file you are having in the directory wont be having the samename.

Explainer Screenshot

Try to rename the Filename as Pandas1 or Pandas_Tutorial.py. It will solve the Problem.

Am_official
  • 467
  • 4
  • 5