Python线程模块导入失败

15 投票
4 回答
17170 浏览
提问于 2025-04-17 05:56

我正在尝试导入线程模块,但总是出现一些莫名其妙的错误。以下是我的代码:

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上运行

4 个回答

0

_thread.start_new_thread(func*)

这个代码的意思是用来启动一个新的线程。线程可以理解为程序中的一个小任务,它可以和其他任务同时进行。这里的“func”是你想要在新线程中运行的函数,也就是你希望这个小任务去做的事情。星号“*”表示这个函数可以接受一些参数,具体的参数内容可以根据需要来传递。

4

首先,你需要把自己的文件改个名字:现在这个文件叫做 threading.py,由于它在 Python 的路径中,所以它会替代标准 Python 库里的 threading 模块。

其次,你需要创建一个你自己线程类的实例:

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

我觉得你只需要改一下你工作文件的名字,因为你的文件名和模块名是一样的:

threading.py

或者你在工作目录里有一个错误的 threading.py 文件。

撰写回答