How to Solve Python ModuleNotFoundError: no module named ‘Queue’

by | Programming, Python, Tips

A common error you may encounter when using Python is modulenotfounderror: no module named ‘Queue’.

This error occurs if you try to import the built-in module queue using import Queue.

All names in Python are case-sensitive, including module names.

You can solve this error by importing the module with import queue or if you want to import the Queue class you can use the from queue import Queue

This tutorial goes through how to solve this error with code examples.


What is ModuleNotFoundError?

The ModuleNotFoundError occurs when the module you want to use is not present in your Python environment. There are several causes of the modulenotfounderror:

The module’s name is incorrect, in which case you have to check the name of the module you tried to import. Let’s try to import the re module with a double e to see what happens:

import ree
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
1 import ree

ModuleNotFoundError: No module named 'ree'

To solve this error, ensure the module name is correct. Let’s look at the revised code:

import re

print(re.__version__)
2.2.1

You may want to import a local module file, but the module is not in the same directory. Let’s look at an example package with a script and a local module to import. Let’s look at the following steps to perform from your terminal:

mkdir example_package

cd example_package

mkdir folder_1

cd folder_1

vi module.py

Note that we use Vim to create the module.py file in this example. You can use your preferred file editor, such as Emacs or Atom. In module.py, we will import the re module and define a simple function that prints the re version:

import re

def print_re_version():

    print(re.__version__)

Close the module.py, then complete the following commands from your terminal:

cd ../

vi script.py

Inside script.py, we will try to import the module we created.

import module

if __name__ == '__main__':

    mod.print_re_version()

Let’s run python script.py from the terminal to see what happens:

Traceback (most recent call last):
  File "script.py", line 1, in ≺module≻
    import module
ModuleNotFoundError: No module named 'module'

To solve this error, we need to point to the correct path to module.py, which is inside folder_1. Let’s look at the revised code:

import folder_1.module as mod

if __name__ == '__main__':

    mod.print_re_version()

When we run python script.py, we will get the following result:

2.2.1

You can also get the error by overriding the official module you want to import by giving your module the same name.

Lastly, you can encounter the modulenotfounderror when you import a module that is not installed in your Python environment.

In this example, queue is a built-in module, which means it comes automatically with Python.

The import of the queue module name is case sensitive, which means queue exists as a built-in Python module but Queue does not.

What is queue?

The queue module implements multi-producer and multi-consumer queues.

Example

Let’s look at an example to reproduce the error. We will attempt to create a queue with a fixed size of five and put five items into the queue.

import Queue

q1 = Queue(5) #The max size is 5.

q1.put(1)

q1.put(2)

q1.put(3)

q1.put(4)

q1.put(5)

print(q1.full()) # will return true.

Let’s run the code to see what happens:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import Queue
      2 q1 = Queue(5) #The max size is 5.
      3 q1.put(1)

ModuleNotFoundError: No module named 'Queue'

The error occurs because Queue is not the correct spelling for the built-in queue module.

Solution

We can solve this error by using the correct name for the queue module, which is in all lowercase. Then we can access the Queue class from the module and create a Queue object. Let’s look at the revised code:

import queue

q1 = queue.Queue(5) #The max size is 5.

q1.put(1)

q1.put(2)

q1.put(3)

q1.put(4)

q1.put(5)

print(q1.full()) # will return true.

Let’s run the code to get the result:

True

We can also use the from keyword to import the class directly at the top of the script. Let’s look at the revised code:

from queue import Queue

q1 = Queue(5) #The max size is 5.

q1.put(1)

q1.put(2)

q1.put(3)

q1.put(4)

q1.put(5)

print(q1.full()) # will return true.

Let’s run the code to see the result:

True

Summary

Congratulations on reading to the end of this tutorial.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

For further reading on missing modules in Python, go to the article:

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!