NameError: name 'csv' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

NameError: name 'csv' is not defined in Python #

The Python "NameError: name 'csv' is not defined" occurs when we use the csv module without importing it first. To solve the error, import the csv module before using it - import csv.

nameerror name csv is not defined

Here is an example of how the error occurs.

main.py
with open('employees.csv', 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['first_name', 'last_name'] # ⛔️ NameError: name 'csv' is not defined writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow({'first_name': 'Alice', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Bob', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Carl', 'last_name': 'Smith'})

To solve the error, we have to import the csv module.

main.py
# ✅ import csv module first import csv with open('employees.csv', 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['first_name', 'last_name'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow({'first_name': 'Alice', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Bob', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Carl', 'last_name': 'Smith'})

Even though the csv module is in the Python standard library, we still have to import it before using it.

Make sure you haven't used a capital letter c when importing csv because module names are case-sensitive.

Also, make sure you haven't imported csv in a nested scope, e.g. a function. Import the module at the top level to be able to use it throughout your code.

An alternative to importing the entire csv module is to import only the functions and classes that your code uses.

main.py
from csv import DictWriter with open('employees.csv', 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['first_name', 'last_name'] writer = DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow({'first_name': 'Alice', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Bob', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Carl', 'last_name': 'Smith'})

The example shows how to import the DictWriter class from the csv module.

Instead of accessing the members on the module, e.g. csv.DictWriter, we now access them directly.

This should be your preferred approach because it makes your code easier to read.

For example, when we use an import such as import csv, it is much harder to see which classes or functions from the csv module are being used in the file.

Conversely, when we import specific classes, it is much easier to see which classes and methods from the csv module are being used.

The csv module implements classes to read and write tabular data in CSV format.

You can view all of the classes and methods the csv module provides by visiting the official docs.

Conclusion #

The Python "NameError: name 'csv' is not defined" occurs when we use the csv module without importing it first. To solve the error, import the csv module before using it - import csv.

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.