You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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-in queue module).
  • 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.py to something unique, like my_queue_demo.py or queue_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 queue module, and you’ll be able to use queue.Queue without 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:17:57