AttributeError 'module' has no attribute 'Queue"
Categories:
Resolving 'AttributeError: module has no attribute Queue' in Python

This article provides a comprehensive guide to understanding and resolving the common 'AttributeError: module has no attribute Queue' in Python, focusing on common causes and solutions.
The AttributeError: module has no attribute Queue
is a common Python error that indicates you're trying to access an attribute named Queue
from a module, but that attribute doesn't exist within the module's namespace. This often happens due to changes in module structure, incorrect imports, or naming conflicts. Understanding the root cause is crucial for a quick resolution.
Understanding the Python queue
Module
Python's standard library includes a queue
module (lowercase 'q') that provides several thread-safe queue implementations. These are essential for concurrent programming, allowing multiple threads to exchange data safely. The primary classes within this module are Queue
, LifoQueue
, and PriorityQueue
.
classDiagram class queue { +Queue() +LifoQueue() +PriorityQueue() +Empty +Full } Queue <|-- LifoQueue Queue <|-- PriorityQueue
Class diagram of the queue
module's main components.
Common Causes and Solutions
This error typically stems from one of a few common scenarios. Identifying which scenario applies to your code is the first step towards a fix.
Cause 1: Incorrect Import Statement
The most frequent cause of this AttributeError
is an incorrect import statement. Developers often confuse how to import classes directly versus importing the module itself and then accessing its members.
Scenario A: Importing the module, but trying to access Queue
directly
If you import the queue
module using import queue
, you must then access its classes using queue.Queue
, queue.LifoQueue
, etc. Trying to use Queue()
directly will result in the AttributeError
because Queue
is not defined in the global namespace.
# Incorrect way
import queue
my_queue = Queue() # AttributeError: name 'Queue' is not defined
# Correct way
import queue
my_queue = queue.Queue()
print(type(my_queue))
Demonstrating incorrect vs. correct module import and class instantiation.
Scenario B: Importing a specific class, but trying to access it via the module name
Conversely, if you use from queue import Queue
to directly import the Queue
class, you should then use Queue()
directly. Attempting to use queue.Queue()
after this type of import will also lead to an AttributeError
because queue
itself is not imported as a module, only the Queue
class from it.
# Incorrect way
from queue import Queue
my_queue = queue.Queue() # AttributeError: name 'queue' is not defined
# Correct way
from queue import Queue
my_queue = Queue()
print(type(my_queue))
Demonstrating incorrect vs. correct direct class import and instantiation.
Cause 2: Naming Conflicts (Shadowing)
Another common issue arises when you have a file or a variable named queue.py
in your project directory, or if you've assigned a variable named queue
that overwrites the imported module. Python's import mechanism will prioritize local files over standard library modules, leading to your queue.py
being imported instead of the built-in queue
module. If your local queue.py
doesn't define a Queue
attribute, you'll get the AttributeError
.
flowchart TD A[Python Interpreter] B{Import 'queue'} C[Check current directory for 'queue.py'] D[Check sys.path for 'queue' module] E[Local 'queue.py' found] F[Standard Library 'queue' found] G[Local 'queue.py' imported] H[Standard 'queue' module imported] I{Access 'Queue' attribute} J[AttributeError: module has no attribute 'Queue'] K[Success: Queue class found] A --> B B --> C C --> E C --> D E --> G D --> F G --> I F --> H H --> I I --> J I --> K
Flowchart illustrating Python's module import resolution and potential shadowing.
# Imagine you have a file named queue.py in the same directory:
# # queue.py content:
# def my_function():
# pass
# Your main script:
import queue
# If queue.py is imported, it won't have a 'Queue' class
# my_queue = queue.Queue() # This would raise the AttributeError
# Solution: Rename your local file or variable to avoid conflict.
# For example, rename queue.py to my_custom_queue.py
# Then import my_custom_queue and the standard queue module separately.
import my_custom_queue # Assuming you renamed your file
import queue
my_queue_instance = queue.Queue()
print(type(my_queue_instance))
Example of a naming conflict and its resolution.
os.py
, math.py
, json.py
, queue.py
). This can lead to subtle and hard-to-debug import errors.Cause 3: Python 2 vs. Python 3 Differences
While less common in modern development, if you're working with older codebases or migrating from Python 2 to Python 3, you might encounter this error. In Python 2, the module was named Queue
(capital 'Q'), and you would import it as import Queue
. In Python 3, it was renamed to queue
(lowercase 'q').
Python 2
Python 2
import Queue my_queue = Queue.Queue() print(type(my_queue))
Python 3
Python 3
import queue my_queue = queue.Queue() print(type(my_queue))
If you're running Python 3 code that was written for Python 2, or vice-versa, this naming difference will cause the AttributeError
.
Troubleshooting Steps
When you encounter this error, follow these steps to diagnose and resolve the issue:
1. Verify your import statement
Double-check how you're importing the queue
module or its classes. Ensure consistency between your import
statement and how you're accessing Queue
.
2. Check for naming conflicts
Search your project directory for any files named queue.py
. If found, rename them to something unique (e.g., my_queue_utils.py
). Also, ensure you haven't assigned a variable named queue
that could shadow the module.
3. Inspect sys.path
You can programmatically check where Python is looking for modules. Add import sys; print(sys.path)
to your script to see the directories Python searches. This can help identify if an unexpected local file is being picked up.
4. Confirm Python version
Run python --version
in your terminal to ensure you're using the expected Python interpreter, especially if you have multiple versions installed.
5. Restart your IDE/environment
Sometimes, IDEs or interactive environments (like Jupyter notebooks) can cache module imports. A simple restart can clear these caches and resolve transient issues.