0

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'
  • Possible duplicate of [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – pppery Jul 30 '17 at 14:39

1 Answers1

1

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
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
  • @hcwhsa: Thanks. Didn't notice the method call by OP. – Nipun Batra Oct 19 '13 at 04:24
  • 1
    The issue was indeed a script ~/csv.py. I have no idea what the file was, the contents were foreign to me. Something from an old tutorial is all I can think. The error made me think there was actually something wrong with the csv module, but now I'm thinking I need to take a moment and review exactly what it is that import declarations do. – user2774088 Oct 19 '13 at 15:10