How To Resolve “AttributeError Module ‘pandas’ Has No Attribute ‘read_csv'” In Python

AttributeError module 'pandas' has no attribute 'read_csv'

To fix the “AttributeError module ‘pandas’ has no attribute ‘read_csv’ error in Python, you can change the filename locally or read the csv file using the csv.reader function. Read the article below.

What causes the error “AttributeError module ‘pandas’ has no attribute ‘read_csv’“?

Example:

I have a csv file named ‘data.csv‘ that contains a dataset like this:

Name, Age, Vacations
John, 18, Student
Peter, 18, Student
Frank, 18,Student

I print this file ‘data.csv’ on another file named ‘csv.py‘.

import pandas
 
# Read the csv file using pandas
exportData = pandas.read_csv('data.csv')
print(exportData)

Output:

File "d:\vscode\Python\csv.py", line 4, in <module>
    exportData = pandas.read_csv('data.csv')
                 ^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)

Or when you name it ‘pandas.py‘, you will get the same error.

Example:

  • I’m still doing a read of the file ‘data.csv’ and a read of it on the file ‘pandas.py‘.
import pandas
 
# Read the csv file using pandas.
exportData = pandas.read_csv('data.csv')
print(exportData)

Output:

File "d:\vscode\Python\pandas.py", line 4, in <module>
    exportData = pandas.read_csv('data.csv')
                 ^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)

In short, the problem causing the error is that you have a local file ‘pandas.py’ and ‘csv.py’ that is preventing the pandas module from working.

How to solve this error?

Delete or change the names of local files.

Local files have error names like ‘pandas.py’ and ‘csv.py’. It would be best if you changed it to something else that doesn’t match the module name.

Example:

  • I changed (or removed) the filename ‘csv.py’ to ‘readcsv.py’.
import pandas
 
# Read the csv file using pandas.
exportData = pandas.read_csv('data.csv')
print(exportData)

Output:

    Name   Age  Vacations
0   John    18    Student
1  Peter    18    Student
2  Frank    18    Student

Example:

  • Do the same for the file ‘pandas.py’. I will rename (or delete it). I named the new file ‘readfilecsv.py’.
import pandas
 
# Read the csv file using pandas.
exportData = pandas.read_csv('data.csv')
print(exportData)

Output:

    Name   Age  Vacations
0   John    18    Student
1  Peter    18    Student
2  Frank    18    Student

Read the csv file on the CSV module

Example:

  • Create a data.csv file containing the tuple.
  • Read the file ‘data.csv’ using the csv.reader function in the csv module.
Name, Age, Vacations
John, 18, Student
Peter, 18, Student
Frank, 18,Student
import csv

with open('pandas.csv','rt')as f:
    # Read csv file using csv.reader function
    exportData = csv.reader(f)
    for i in exportData:
        print(i)

Output:

['Name', ' Age', ' Vacations']
['John', ' 18', ' Student']
['Peter', ' 18', ' Student ']
['Frank', ' 18', 'Student']

Summary

My post is over, and the AttributeError module ‘pandas’ has no attribute ‘read_csv’ has been solved. The point here is that you must avoid blocking or dynamic names of the module. Hope the article is helpful to you.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *