How to solve NameError: name ‘csv’ is not defined in Python

The error “NameError: name ‘csv’ is not defined” in Python occurs because you use the classes function from the csv module without importing it. This tutorial will give you popular mistakes that cause the error and show you how to solve them.

The error “NameError: name ‘csv’ is not defined” in Python

The csv module provides methods to work with CSV files. When you use the methods without importing the csv module, the system will notice “NameError: name ‘csv’ is not defined”. For example, we will use the reader class to read a CSV file without importing the csv module:

Code:

from contextlib import closing
import requests

# URL to read CSV file directly
url = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/planets.csv"

with closing(requests.get(url, stream=True)) as f:
    line = (line.decode('utf-8') for line in f.iter_lines())
    data = csv.reader(line)

    # Loop over the csv.reader object to get elements
    for element in data:
        print(element)

Result:

NameError                                 Traceback (most recent call last)
 in <module>
      8     line = (line.decode('utf-8') for line in f.iter_lines())
----> 9     data = csv.reader(line)
NameError: name 'csv' is not defined

Solution to the error

Import the csv module

We explained to you the root of the problem in the first part. So it is easy to fix the error. The only thing you should do is import the csv module before using its classes. Look at the solution below:

Code:

import csv # <-- Import the csv module
from contextlib import closing
import requests
 
# URL to read CSV file directly
url = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/planets.csv"
 
with closing(requests.get(url, stream=True)) as f:
    line = (line.decode('utf-8') for line in f.iter_lines())
    data = csv.reader(line)
 
    # Loop over the csv.reader object to get elements
    for element in data:
        print(element)

Result:

['Planet', 'Color']
['Mercury', 'Grey']
['Venus', 'Brown and Grey']
['Earth', 'Blue, Brown Green and White']
['Mars', 'Red, Brown and Tan']
['Jupiter', 'Brown, Orange and Tan, with White cloud stripes']
['Saturn', 'Golden, Brown, and Blue-Grey']
['Uranus', 'Blue-Green']
['Neptune', 'Blue']

Import the classes directly 

Alternatively, you can import the classes you will use to work with the csv files in the beginning when importing other libraries. The following code is an example:

Code:

from csv import reader # <-- Import the reader class from the csv module
from contextlib import closing
import requests
 
# URL to read CSV file directly
url = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/planets.csv"
 
with closing(requests.get(url, stream=True)) as f:
    line = (line.decode('utf-8') for line in f.iter_lines())
    
# Use reader instead of csv.reader
    data = reader(line)
 
    # Loop over the csv.reader object to get elements
    for element in data:
        print(element)

Result:

['Planet', 'Color']
['Mercury', 'Grey']
['Venus', 'Brown and Grey']
['Earth', 'Blue, Brown Green and White']
['Mars', 'Red, Brown and Tan']
['Jupiter', 'Brown, Orange and Tan, with White cloud stripes']
['Saturn', 'Golden, Brown, and Blue-Grey']
['Uranus', 'Blue-Green']
['Neptune', 'Blue']

Summary

In summary, the error “NameError: name ‘csv’ is not defined” occurs because you use classes from the csv module to work with csv files without importing the module. To get rid of the error, remember to import the csv module before using its classes or import the classes directly in the beginning.

Leave a Reply

Your email address will not be published. Required fields are marked *