0

当我运行以下代码时,我得到了一个 AttributeError:

#!/usr/bin/env python

import threading
import time

def worker():
    for i in range(10):
        time.sleep(1) # Seconds
        print i

threading.Thread(target=worker).start()

错误:

Traceback (most recent call last):
  File "/home/yaa110/workspace/Python27/src/threading.py", line 3, in <module>
    import threading
  File "/home/yaa110/workspace/Python27/src/threading.py", line 11, in <module>
    threading.Thread(target=worker).start()
AttributeError: 'module' object has no attribute 'Thread'

我在 Ubuntu 13.10 上运行 Python 2.7.5。此外,当我通过终端使用 python 时,我通过逐行输入代码没有错误。

4

1 回答 1

5

看起来您已经在此脚本的当前目录中创建了一个名为threadingthreading.py. 这会导致您import threading导入错误的线程库;其中没有名为 Thread 的模块!

重命名它,一切都应该没问题。在我的 2.7.2 上运行脚本效果很好!

于 2013-10-31T22:11:02.943 回答