If you are a Python developer, you might have encountered the error message “NameError: name ‘heapq’ is not defined” at some point in your coding journey. This error occurs when you try to use the heapq
module in your code, but Python cannot find it.

In this tutorial, we will discuss the common causes of this error and how to fix it.
Why does the NameError: name 'heapq' is not defined
error occur?
This error occurs when you try to use the heapq
module in your Python code, but Python cannot find the heapq
module in its namespace. The following are some of the scenarios in which this error usually occurs.
- You have not imported the heapq module.
- You have imported the heapq module using a different name.
How to fix the NameError: name 'heapq' is not defined
?
heapq
is a Python module that provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. The heapq
module provides functions to create and manipulate heaps. The heapq
module is useful for solving problems that involve finding the smallest or largest elements in a collection. It is also used in graph algorithms such as Dijkstra’s algorithm for finding the shortest path in a graph.
Since this module is part of the Python Standard Library, you don’t need to separately install it. You can simply import it and start using it.
Let’s now look at the above scenarios that may result in the above error in detail.
The heapq
module is not imported
It can happen that you are trying to use the heapq
module without even importing it. This is because Python does not recognize the heapq
library and its functions until it is imported into the code.
For example, let’s try to use the heapq
module without importing it and see what we get.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# note that the heapq module is not imported # create a list of some numbers ls = [1, 5, 2, 3, 4, 6, 7] # convert the list to a heap (a min heap to be exact), this method is modifies the list in-place heapq.heapify(ls) # pop and get the smallest value in the heap print(heapq.heappop(ls))
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 7 4 ls = [1, 5, 2, 3, 4, 6, 7] 6 # convert the list to a heap (a min heap to be exact), this method is modifies the list in-place ----> 7 heapq.heapify(ls) 9 # pop and get the smallest value in the heap 10 print(heapq.heappop(ls)) NameError: name 'heapq' is not defined
We get a NameError
stating that the name heapq
is not defined. To use the heapq
library, you need to import it first.
import heapq # create a list of some numbers ls = [1, 5, 2, 3, 4, 6, 7] # convert the list to a heap (a min heap to be exact), this method is modifies the list in-place heapq.heapify(ls) # pop and get the smallest value in the heap print(heapq.heappop(ls))
Output:
1
Here, we are importing the heapq
module first and then using it to convert a list to a min heap and then pop and get the smallest value in the min heap. You can see that we did not get any errors here.
The heapq
module is imported using a different name
If you import the heapq
module using a different name, for example import heapq as hq
, and then try to use the name “heapq” to use it, you will get a NameError
because the name “heapq” is not defined in your current namespace.
Let’s look at an example.
import heapq as hq # create a list of some numbers ls = [1, 5, 2, 3, 4, 6, 7] # convert the list to a heap (a min heap to be exact), this method is modifies the list in-place heapq.heapify(ls) # pop and get the smallest value in the heap print(heapq.heappop(ls))
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 7 4 ls = [1, 5, 2, 3, 4, 6, 7] 6 # convert the list to a heap (a min heap to be exact), this method is modifies the list in-place ----> 7 heapq.heapify(ls) 9 # pop and get the smallest value in the heap 10 print(heapq.heappop(ls)) NameError: name 'heapq' is not defined
We get a NameError: name 'heapq' is not defined
. This is because we have imported the heapq
module with the name hq
but we’re trying to use it using the name heapq
.
To fix this error, you can either access heapq
using the name that you have used in the import statement or import heapq
without an alias.
import heapq as hq # create a list of some numbers ls = [1, 5, 2, 3, 4, 6, 7] # convert the list to a heap (a min heap to be exact), this method is modifies the list in-place hq.heapify(ls) # pop and get the smallest value in the heap print(hq.heappop(ls))
Output:
1
In the above example, we are importing datetime
as hq
and then using hq
to access the heapq
module’s methods.
Alternatively, as seen in the example in the previous section, you can import heapq
without any aliases and simply use heapq
to avoid the NameError
.
Conclusion
In conclusion, encountering a NameError: name 'heapq' is not defined
error can be frustrating, but it is a common issue that can be easily fixed. By following the steps outlined in this tutorial, you should now have a better understanding of what causes this error and how to resolve it. Remember to always check your code for typos and syntax errors, and to import any necessary modules before using them in your code. With these tips in mind, you should be able to tackle any NameError
errors that come your way.
You might also be interested in –