我有一个在树莓派上运行的小型python项目。它监控温度,检查和发送电子邮件,并控制PDU。它完全从包含在raspbian中的Thonny IDE运行。但是,当我从命令行运行它时,或者理想情况下在启动时,它会在处理检查电子邮件的一个特定代码段中失败。
我使用pip install在系统上安装了电子邮件模块。然后我意识到这是python中的标准,所以我卸载了它。
这是错误代码。
Traceback (most recent call last):
File "/home/pi/Documents/Python Files/Temp_Monitor_Project/temp_controller.py", line 122, in <module>
main()
File "/home/pi/Documents/Python Files/Temp_Monitor_Project/temp_controller.py", line 104, in main
check_email_com(state_and_monitoring)
File "/home/pi/Documents/Python Files/Temp_Monitor_Project/temp_controller.py", line 16, in check_email_com
command = check_email.check_for_commands()
File "/home/pi/Documents/Python Files/Temp_Monitor_Project/check_email.py", line 43, in check_for_commands
command = message.checkMail()
File "/home/pi/Documents/Python Files/Temp_Monitor_Project/check_email.py", line 20, in checkMail
email_msg = email.message_from_bytes(data[0][1])
AttributeError: 'module' object has no attribute 'message_from_bytes'
这是它失败的脚本。
import imaplib, struct, time, email, Send_Email
#global sender
class Mail():
def __init__(self):
self.user= 'email address'
self.password= 'password'
self.M = imaplib.IMAP4_SSL('imap.gmail.com', '993')
try:
self.M.login(self.user, self.password)
except:
print("mail login failed")
def checkMail(self):
self.M.select()
self.unRead = self.M.search(None, '(SUBJECT "Temp Monitor Command" UnSeen)')
if len(self.unRead[1][0].split()) > 0:
status, data = self.M.fetch(self.unRead[1][0], '(RFC822)')
email_msg = email.message_from_bytes(data[0][1])
if email_msg.is_multipart():
for part in email_msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True)
body = body.decode()
elif part.get_content_type() == "text/html":
continue
#print(self.M.fetch(self.unRead[1][0], "(BODY[HEADER.FIELDS (FROM)])"))
return body
else:
return -1
答案 0 :(得分:0)
我认为问题出在您尝试执行代码的附近环境中。尝试在两种情况下检查已安装的模块。您可以阅读有关如何在执行期间列出已使用模块的here。您还应该检查两种情况下使用的python实例。为此,您可以执行此
import sys
print(sys.executable)
print(sys.version)