2

I tried many times but it showing same error, i dont know where is did mistake..

import csv

with open('student.csv','w',newline='') as f:
   w = csv.writer(f)
   w.writerow(['Rollno','Name','Marks'])
   w.writerow([101,'somesh',89])
   w.writerow([102,'yogesh',85])
   w.writerow([103,'akhilesh',75])
   w.writerow([874,'supreet',87])
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • What you have pasted is not legal python, the indentation is wrong. – Tim Richardson Mar 31 '21 at 19:21
  • 7
    You have most likely named this (or some other script of yours) `csv.py`, so that the `import csv` is reading that instead of the actual module with that name. – jasonharper Mar 31 '21 at 19:29
  • thanks @jasonharper i gave different file name insted of csv.py , now its working, – Somesh Hiremath Mar 31 '21 at 19:36
  • Does this answer your question? [Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name”](https://stackoverflow.com/q/36250353/6045800) – Tomerikoo Aug 25 '21 at 10:06

2 Answers2

6

This error is often related to the name of the .py file, as it is named as csv.py. When it tries to import csv, it imports its csv.py file which does not contain any csv.writer functions. This can be solved by simply changing the name of your .py file.

iamgonzalez
  • 116
  • 1
  • 8
2

Just rename your csv.py file to csv_test.py for example.

ahmnouira
  • 1,607
  • 13
  • 8