Skip to main content

How to Resolve Python "AttributeError: module 'csv' has no attribute 'reader/writer'"

When working with CSV files in Python using the standard csv module, encountering AttributeError: module 'csv' has no attribute 'reader' or AttributeError: module 'csv' has no attribute 'writer' can be puzzling. These errors almost always stem from a simple but common mistake: naming your own script file csv.py.

This guide explains why this filename conflict causes the error and provides the straightforward solution.

Understanding the Error: Module Shadowing

Python's import system searches for modules in a specific order, starting with the current directory. If you create a file named csv.py in your project folder and then try to import csv within that same file (or another file in the same folder), Python finds your local csv.py first. It imports your script instead of the built-in csv module from the standard library.

Since your local csv.py file doesn't contain the definitions for the reader or writer functions (unless you explicitly defined them, which is unlikely in this context), trying to access csv.reader or csv.writer results in an AttributeError. Your file effectively "shadows" the real csv module.

The Primary Cause: A Local csv.py File

Having a file named csv.py in your working directory is the overwhelming cause of this specific AttributeError.

csv.py
# ⛔️ Problematic Code - IF THIS FILE IS NAMED csv.py ⛔️
import csv # This imports the file itself, not the standard library module!
import io

# Example CSV data as a string for demonstration
csv_data_read = """header1,header2
value1,value2
value3,value4"""

csv_data_write = [['colA', 'colB'], [1, 2]]

# --- Attempting to use csv.reader ---
try:
file_like_read = io.StringIO(csv_data_read)
# ⛔️ AttributeError: module 'csv' has no attribute 'reader'
csv_reader = csv.reader(file_like_read)
for row in csv_reader:
print(f"Read row: {row}")
except AttributeError as e:
print(f"Reader Error: {e}")

# --- Attempting to use csv.writer ---
try:
file_like_write = io.StringIO()
# ⛔️ AttributeError: module 'csv' has no attribute 'writer'
csv_writer = csv.writer(file_like_write)
csv_writer.writerows(csv_data_write)
print(f"Written content attempt: {file_like_write.getvalue()}")
except AttributeError as e:
print(f"Writer Error: {e}")

Running the code above when the file is saved as csv.py will produce the AttributeError for both reader and writer.

The Solution: Rename Your Local csv.py File

The fix is direct: Rename your Python script to something that doesn't clash with standard library module names. Good choices include process_csv.py, main.py, app.py, or a name reflecting the script's purpose.

main.py
# ✅ Corrected Code - Saved as main.py (or any other non-conflicting name) ✅
import csv # Now correctly imports the standard library csv module
import io

# Example CSV data
csv_data_read = """header1,header2
value1,value2
value3,value4"""

csv_data_write = [['colA', 'colB'], [1, 2]]

print("--- Testing csv.reader ---")
try:
file_like_read = io.StringIO(csv_data_read)
# ✅ Works now
csv_reader = csv.reader(file_like_read)
for row in csv_reader:
print(f"Read row: {row}")
except AttributeError as e:
print(f"Unexpected Reader Error: {e}") # Should not happen now
except Exception as e:
print(f"Other Read Error: {e}")

print("--- Testing csv.writer ---")
try:
file_like_write = io.StringIO()
# ✅ Works now
csv_writer = csv.writer(file_like_write)
csv_writer.writerows(csv_data_write)
print(f"Written content: {file_like_write.getvalue()}")
except AttributeError as e:
print(f"Unexpected Writer Error: {e}") # Should not happen now
except Exception as e:
print(f"Other Write Error: {e}")

After renaming your file, the import csv statement will correctly find the standard library module, making csv.reader and csv.writer available.

Debugging: Confirming the Filename Conflict

If you're unsure if a filename conflict is the issue, you can verify which csv module Python loaded:

Checking the Imported Module's File Path (__file__)

Print the __file__ attribute right after importing csv.

import csv

# ✅ Add this line for debugging
print(f"Imported csv module path: {csv.__file__}")

# ... rest of your code ...
  • Problem: If the output path points to your project directory and ends with csv.py (e.g., /path/to/your/project/csv.py), you've imported your local file.
  • Correct: If the path points to your Python installation's standard library (e.g., /usr/lib/python3.10/csv.py, /opt/anaconda3/lib/python3.9/csv.py, or similar within site-packages or a virtual environment), the correct module is loaded.

Checking the Imported Module's Attributes (dir())

List the available attributes using dir(csv).

import csv

# ✅ Add this line for debugging
print(f"Attributes in imported csv: {dir(csv)}")

# ... rest of your code ...
  • Problem: If the output is a short list (mainly __name__, __file__, etc., plus any functions/variables you defined in your csv.py), it's the wrong module. It will lack reader, writer, DictReader, QUOTE_ALL, etc.
  • Correct: If the correct library is imported, dir(csv) will show a longer list including reader, writer, DictReader, DictWriter, dialect constants (QUOTE_MINIMAL, QUOTE_NONNUMERIC), register_dialect, etc.

If you try to import csv within a file that is itself named csv.py, you might encounter a slightly different error:

AttributeError: partially initialized module 'csv' has no attribute 'reader' (most likely due to a circular import)

This occurs because the file is trying to import itself during its own initialization. Python detects this circular dependency and stops full initialization, meaning functions like reader or writer aren't defined yet when your code tries to use them.

The solution is exactly the same: Rename your csv.py file.

Conclusion

The AttributeError: module 'csv' has no attribute 'reader' or 'writer' is almost always caused by naming your own Python script csv.py. This local file "shadows" the standard library csv module, preventing Python from finding the built-in reader and writer functions.

The fix is simple: Rename your local csv.py file to something unique that doesn't conflict with standard module names (e.g., csv_handler.py, main.py). Use csv.__file__ and dir(csv) to confirm if you are importing the wrong file.