user2774088
user2774088

Reputation: 13

AttributeError on import csv

I'm just at a total loss and can't find anything I understand as being relevant either here at SO or with google.

>>> import csv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "csv.py", line 6, in <module>
r = csv.read(f)
AttributeError: 'module' object has no attribute 'read'

Upvotes: 0

Views: 842

Answers (1)

Nipun Batra
Nipun Batra

Reputation: 11387

Your Python script is named csv.py. You need to rename it to something else. Never name your script with the same name as a module.

  1. Change the name of your script
  2. Delete the csv.pyc also from the location where you created your csv.py file.

Should work after this.

Further, as pointed out in comments, csv module does not have a read() method.

Small example from official documentation

>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

Upvotes: 1

Related Questions