How To Resolve AttributeError module ‘csv’ has no attribute ‘reader’ in Python

What causes and ways to resolve the error AttributeError module ‘csv’ has no attribute ‘reader’ in Python. If you are looking for a solution to this problem then this article is for you.

What causes the AttributeError module ‘csv’ has no attribute ‘reader’ error?

The syntax to use the csv.reader function to read CSV files in Python:

csv.reader(f)

The csv.reader() function returns an iterator that contains lists with each list being the contents of a single line read from the CSV file.

The error AttributeError module ‘csv’ has no attribute ‘reader’ occurs because you create a local file with the same name as the module you are importing, resulting in the operation of that module being affected.

Example

Name, Age, Vacations
John, 18, Student
Peter, 18, Student
Frank, 18,Student
import csv
import os.path

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'infor.csv')

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

Output:

Traceback (most recent call last):
  File "d:\vscode\Python\csv.py", line 1, in <module>
    import csv
  File "d:\vscode\Python\csv.py", line 9, in <module>
    exportData = csv.reader(f)
                 ^^^^^^^^^^
AttributeError: partially initialized module 'csv' has no attribute 'reader' (most likely due to a circular import)

The main cause is that the local file ‘csv.py’ hides the functionality of the csv module.

How to solve this error?

Delete or change the names of local files.

To handle the error, change the local filename ‘csv.py’ to something else that does not match the module name.

Example:

  • I will change the filename ‘csv.py’ to ‘readfilecsv.py’.
import csv
import os.path

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'infor.csv')

with open(filename, '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']

Use pandas module.

You can use the pandas module to read a csv file in Python. To read the csv file, use the pandas.read_csv function (csv file name). The pandas.read_csv function will return a table of data read from the csv file.

Example:

import pandas
import os.path

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'infor.csv')

with open(filename, 'rt') as f:
   # Read the csv file using pandas.
    exportData = pandas.read_csv(filename)
    print(exportData)

Output:

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

Note: You also must not name the local file with the name of the pandas module while using it. This also results in the error AttributeError module ‘pandas’ has no attribute ‘read_csv’.

Summary

My post is over, and the AttributeError module ‘csv’ has no attribute ‘reader’ 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.

Leave a Reply

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