Python 窗口线程

-1 投票
4 回答
580 浏览
提问于 2025-04-16 09:20

我在Windows上导入一些东西:

from threading import Thread   

但是它给我报错:

cannot import name Thread

有没有人能帮我解决这个问题?

4 个回答

1

在这里运行得很好:

C:\Program Files (x86)\Console2>python
Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from threading import Thread
>>>

你用的是哪个版本呢?

2

把你的代码改成这样:

import threading
print "threading imported from", threading.__file__

如果第一行出错了,那说明你的安装有问题,可能是配置不对或者缺少了这个库。如果第一行没问题,第二行会告诉你实际加载了哪个文件。通常这会显示它正在导入错误的文件,你可以利用这些信息进一步排查问题。

我猜可能是你当前目录下有一个叫“threading.py”的文件,但这只是根据不够的信息做的猜测。

2

可能是你的 Python 文件叫做 threading.py?如果是这样的话,试着把它改个名字。

撰写回答