Module 'csv' has no attribute 'reader' or 'writer' in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. Module 'csv' has no attribute 'reader' in Python
  2. Module 'csv' has no attribute 'writer' in Python

# Module 'csv' has no attribute 'reader' in Python

The Python "AttributeError module 'csv' has no attribute 'reader'" occurs when we have a local file named csv.py and try to import from the csv module.

To solve the error, make sure to rename any local files named csv.py.

attributeerror module csv has no attribute reader

Here is an example of how the error occurs in a file called csv.py.

csv.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: # ⛔️ AttributeError: module 'csv' has no attribute 'reader' csv_reader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in csv_reader: print(', '.join(row))

The most likely cause of the error is having a local file named csv.py which shadows the csv module from the standard library.

# Make sure you don't have a local file named csv.py

Make sure to rename your local file to something other than csv.py to solve the error.

main.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: # ✅ Works csv_reader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in csv_reader: print(', '.join(row))

You can rename your file to main.py or use any other name that doesn't clash with a module.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import csv print(csv.__file__) # ⛔️ The result is shadowed by a local file # /home/borislav/Desktop/bobbyhadz_python/csv.py # ✅ The result is pulling in the correct module # /usr/lib/python3.10/csv.py

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

Here is what printing the attributes of the csv module looks like when I have a file csv.py in the same directory.

main.py
import csv # ⛔️ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(csv))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you will get the "AttributeError: module has no attribute".

We can see that the imported csv module doesn't have a reader attribute, which makes it evident that we are shadowing the official csv module with our local csv.py file.

If you try to import the csv module in a file called csv.py, you will get a little different error message that means the same thing.

csv.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: # ⛔️ AttributeError: partially initialized module 'csv' has no attribute 'reader' (most likely due to a circular import) csv_reader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in csv_reader: print(', '.join(row))

Renaming your file solves the error.

You can use the sys module to print all of the built-in module names if you ever wonder if your local modules are clashing with built-in ones.

main.py
import sys # 👇️ print all built-in module names print(sys.builtin_module_names)

# Module 'csv' has no attribute 'writer' in Python

The Python "AttributeError module 'csv' has no attribute 'writer'" occurs when we have a local file named csv.py and try to import from the csv module.

To solve the error, make sure to rename any local files named csv.py.

attributeerror module csv has no attribute writer

Here is an example of how the error occurs in a file called csv.py.

csv.py
import csv with open('example.csv', 'w', newline='') as csvfile: # ⛔️ AttributeError: module 'csv' has no attribute 'writer' csv_writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) csv_writer.writerow(['Alice', 'Bob', 'Carl'])

The most likely cause of the error is having a local file named csv.py which shadows the csv module from the standard library.

# Make sure you haven't named your file csv.py

Make sure to rename your local file to something other than csv.py to solve the error.

main.py
import csv with open('example.csv', 'w', newline='', encoding='utf-8') as csvfile: # ✅ Works csv_writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) csv_writer.writerow(['Alice', 'Bob', 'Carl'])

If you have a file named csv.py in your project, rename it to my_csv.py or main.py.

Any name that doesn't clash with a built-in module works.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import csv print(csv.__file__) # ⛔️ The result is shadowed by a local file # /home/borislav/Desktop/bobbyhadz_python/csv.py # ✅ The result is pulling in the correct module # /usr/lib/python3.10/csv.py

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

# Checking what attributes the csv module has

Here is what printing the attributes of the csv module looks like when I have a file csv.py in the same directory.

main.py
import csv # ⛔️ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(csv))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you will get the error.

We can see that the imported csv module doesn't have a writer attribute, which makes it evident that we are shadowing the official csv module with our local csv.py file.

If you try to import the csv module in a file called csv.py, you would get a little different error message that means the same thing.

csv.py
import csv with open('example.csv', 'w', newline='') as csvfile: # ⛔️ AttributeError: partially initialized module 'csv' has no attribute 'writer' (most likely due to a circular import) csv_writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) csv_writer.writerow(['Alice', 'Bob', 'Carl'])

Renaming your file solves the error.

You can use the sys module to print all of the built-in module names if you ever wonder if your local modules are clashing with built-in ones.

main.py
import sys # 👇️ print all built-in module names print(sys.builtin_module_names)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.