How To Fix The Error NameError: Name ‘xlrd’ Is Not Defined In Python?

If you suddenly get the “NameError: name ‘xlrd’ is not defined” in Python when working with an Excel file. Don’t worry, this error is just a small error. Continue reading this article to know how to fix it.

Why does the “NameError: name ‘xlrd’ is not defined” in Python occur?

Xlrd allows us to work with all data from the Excel files in the old .xls format or the new .xlsx format.

The NameError: name ‘xlrd’ is not defined in Python and appears when we don’t import xlrd but try to use it. So when we run the program, that error line will appear. See the example below to understand better.

Let’s say we have an Excel file named bookmark.xls like this:

And we want to get the total number of rows and columns in this file using Python. Like this:

# Read bookmark file
book = xlrd.open_workbook("bookmark.xls")
bookmark = book.sheet_by_index(0)

# Get the total rows and columns in the bookmark file
print("Total of rows is {0}".format(bookmark.nrows))
print("Total of columns is {0}".format(bookmark.ncols))

But when we run the code, we get this error message:

NameError: name 'xlrd' is not defined

How to fix this error?

We will introduce you to the two most straightforward ways to fix the above error.

Install and import xlrd by pip

The xlrd module is not installed with Python by default. So, to import xlrd, we have to install xlrd using the command line below:

pip install xlrd

If you use Python 3 on Linux or macOS, try the following command line:

pip3 install xlrd

Now, to fix the error, we need to import xlrd in the first line of the Python file. See the code below for a better understanding.

import xlrd

# Read bookmark file
book = xlrd.open_workbook("bookmark.xls")
bookmark = book.sheet_by_index(0)

# Get the total rows and columns in the bookmark file
print("Total of rows is {0}".format(bookmark.nrows))
print("Total of columns is {0}".format(bookmark.ncols))

Output:

Total of rows is 5
Total of columns is 2

As you can see, we no longer get the NameError.

Install and import xlrd manually

Try this approach if you tried installing xlrd by pip, but it didn’t work.

This time we will not use pip anymore but install xlrd manually.

First, you have to download the xlrd .gz package here.

[OSDrive]:\Users\[YourPCName]\AppData\Local\Programs\Python\Python310\Lib\site-packages

Next, we unzip the file and get a folder like this:

At this point, we open a terminal in the xlrd directory and type:

python setup.py install

Now, all we need to do is import xlrd and use it.

import xlrd

# Read bookmark file
book = xlrd.open_workbook("bookmark.xls")
bookmark = book.sheet_by_index(0)

# Get the total rows and columns in the bookmark file
print("Total of rows is {0}".format(bookmark.nrows))
print("Total of columns is {0}".format(bookmark.ncols))

Output:

Total of rows is 5
Total of columns is 2

Summary

We have shown you 2 simple ways to fix the “NameError: name ‘xlrd’ is not defined” in Python. Remember to try the first way. If it doesn’t work, then try the second way. We hope this article is helpful to you.

Have a good day!

Maybe you are interested: