Python导入queue模块报错AttributeError:无Queue属性求助
Fixing AttributeError with queue.Queue in Python
Hey there! That error is super common when you accidentally name your own script the same thing as a Python standard library module. Let me break this down for you and help you fix it quickly.
What's causing the error?
The error AttributeError: partially initialized module 'queue' has no attribute 'Queue' (most likely due to a circular import) happens because:
- You probably named your own Python script
queue.py(the exact same name as Python's built-inqueuemodule). - When you run
import queue, Python checks your current directory first for matching files—so it ends up importing your own script instead of the real standard library module. - Since your script is still being initialized when you try to access
queue.Queue, it hasn’t fully loaded the actual standard library module, leading to this confusing error.
How to fix it
Follow these simple steps:
- Rename your script: Change your file’s name from
queue.pyto something unique, likemy_queue_demo.pyorqueue_test_script.py. This eliminates the name conflict with the standard library. - Clear the cache folder: Delete the
__pycache__directory in your project folder (if it exists). Python creates this to cache module files, and old cached data might still cause issues even after renaming. - Re-run your code: Your import will now correctly load Python’s built-in
queuemodule, and you’ll be able to usequeue.Queuewithout errors.
Corrected code example
Here’s your code with a small clarification about qsize() (it returns the current number of items in the queue, not the maxsize):
import queue # Initializing a queue with maxsize 20 qW = queue.Queue(maxsize=20) # qsize() returns the current number of items in the Queue (starts at 0) print(qW.qsize())
内容的提问来源于stack exchange,提问作者Gordon Rose
相关产品推荐
相关产品推荐

