【Python】import thread 导入失败:ModuleNotFoundError: No module named 'thread'


出现问题:引用thread 失败

import thread 导入失败

>>> import thread
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'thread'

 

pip install thread

C:\Users\xxx>pip install thread
Looking in indexes: http://pypi.douban.com/simple
ERROR: Could not find a version that satisfies the requirement thread
ERROR: No matching distribution found for thread

 

解决方法:

import _thread

>>> import _thread
>>> dir()  # 查看已导入模块
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_thread']

 

原因:python3中thread被threading代替,thread被改名为_thread

#兼容python2和python3的写法:

import sys

#如果版本号是3
if(sys.version[:1] == "3"):import _thread as thread

#否则直接引用
else:import thread

 

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM