【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