【问题标题】:Python threading module import failurePython线程模块导入失败
【发布时间】:2011-12-25 03:15:03
【问题描述】:

我正在尝试导入线程模块,但是,我似乎无缘无故地出错了。这是我的代码:

import threading

class TheThread ( threading.Thread ):
    def run ( self ):
        print 'Insert some thread stuff here.'
        print 'I\'ll be executed...yeah....'
        print 'There\'s not much to it.'

TheThread.Start()

还有错误:

Traceback (most recent call last):
  File "threading.py", line 1, in <module>
    import threading
  File "C:\Users\Trent\Documents\Scripting\Python\Threading\threading.py", line
3, in <module>
    class TheThread ( threading.Thread ):
AttributeError: 'module' object has no attribute 'Thread'
Press any key to continue . . .

Python 统计数据:

Python 2.7.2(默认,2011 年 6 月 12 日,15:08:59)[MSC v.1500 32 位 (英特尔)] 在 win 32 上

【问题讨论】:

  • 您的当前目录中是否有名为“threading.py”的文件?如果是这样,这可能是没有 Thread 属性的原因。
  • 如果之前运行过代码记得删除threading.pyc

标签: python multithreading import


【解决方案1】:

我认为您在当前工作文件中使用了线程名称。将文件名更改为不同的名称。它会起作用。它也适用于我

【讨论】:

    【解决方案2】:

    _thread.start_new_thread(func*)

    【讨论】:

    • 这是 python 3 的新语法
    【解决方案3】:

    首先,您必须重命名您自己的文件:它被称为 threading.py,由于它位于 Python 路径中,它替换了标准 Python 库的线程模块。

    其次,您必须创建线程类的实例:

    TheThread().start() # start with latter case
    

    【讨论】:

      【解决方案4】:

      我认为您只需要重命名工作文件的名称,因为您的文件名与模块名称相同:

      threading.py

      或者你的工作目录中有错误的 threading.py 文件

      【讨论】:

      • 当我在上面留下评论时,我很困惑为什么这个答案不存在。不管怎样,你打败了我。
      • 愚蠢的我没有注意到该文件与模块同名:/
      • 泰勒,我回答了,然后临时删除了答案以在我的计算机上模拟这种情况:)
      猜你喜欢
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多