首页 > 解决方案 > NameError:未定义名称“线程”将在终端中运行 Python 程序

问题描述

我在我的 python 文件中导入了以下模块:-

from socket import *
from threading import *

但是,我仍然收到此错误:-

NameError: name 'threading' is not defined

它在这一行显示错误:-

receive_thread = threading.Thread(target=receive)

谁能让我知道我该如何解决这个问题?任何帮助将不胜感激!谢谢:)

标签: python

解决方案


如果您导入from threading import *,则必须直接访问这些方法,而无需threading先调用该类。因此,您只需从线程中导入每个方法并使用它们:

from threading import *
receive_thread = Thread(target=receive)

否则导入然后引用模块本身

import threading
receive_thread = threading.Thread(target=receive)

推荐阅读