Python Raspberry pi脚本属性错误:模块';日期时间';没有属性';现在';
我在pi上运行脚本时遇到此错误。我尝试导入datetime,但也出现了错误。我是否缺少缩进或其他函数Python Raspberry pi脚本属性错误:模块';日期时间';没有属性';现在';,python,python-3.x,raspberry-pi,home-assistant,Python,Python 3.x,Raspberry Pi,Home Assistant,我在pi上运行脚本时遇到此错误。我尝试导入datetime,但也出现了错误。我是否缺少缩进或其他函数 2020-07-20 13:26:56-[INFO]门铃:监听GPIO 27上的代码 已连接结果代码0 12 384450 拍照 回溯(最近一次呼叫最后一次): 文件“doorbell.py”,第79行,在 post_image() 文件“doorbell.py”,第20行,在post_图像中 文件\u name='image'+str(datetime.now())+'.jpg' Attrib
2020-07-20 13:26:56-[INFO]门铃:监听GPIO 27上的代码
已连接结果代码0 12 384450
拍照
回溯(最近一次呼叫最后一次):
文件“doorbell.py”,第79行,在
post_image()
文件“doorbell.py”,第20行,在post_图像中
文件\u name='image'+str(datetime.now())+'.jpg'
AttributeError:模块“datetime”没有“now”属性
我已经通过使用sudo nohup python3 doorbell.py按响了脚本。这是脚本
# combine the MQTT and RF receive codes
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import picamera
import argparse
import signal
import sys
import time
import datetime
import logging
from rpi_rf import RFDevice
rfdevice = None
### camera
camera = picamera.PiCamera()
camera.vflip=True
#
def post_image():
print('Taking photo')
camera.capture('image.jpg')
file_name = 'image_' + str(datetime.now()) + '.jpg'
camera.capture(file_name) # time-stamped image
with open('image.jpg', "rb") as imageFile:
myFile = imageFile.read()
data = bytearray(myFile)
client.publish('dev/camera', data, mqttQos, mqttRetained) #
client.publish('dev/test', 'Capture!') # to trigger an automation later
print(file_name + 'image published')
#
### MQTT
broker = '192.168.1.15'
topic ='dev/test'
mqttQos = 0
mqttRetained = False
#
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(topic)
# The callback for when a PUBLISH message is received from the server.
#
def on_message(client, userdata, msg):
payload = str(msg.payload.decode('ascii')) # decode the binary string
print(msg.topic + " " + payload)
process_trigger(payload)
#
def process_trigger(payload):
if payload == 'ON':
print('ON triggered')
post_image()
#
client = mqtt.Client()
client.on_connect = on_connect # call these on connect and on message
client.on_message = on_message
client.username_pw_set(username='',password='') # need this
client.connect(broker)
client.loop_start() # run in background and free up main thread
### RF
#
def exithandler(signal, frame):
rfdevice.cleanup()
sys.exit(0)
logging.basicConfig(level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S',
format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', )
parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device')
parser.add_argument('-g', dest='gpio', type=int, default=27,
help="GPIO pin (Default: 27)")
args = parser.parse_args()
signal.signal(signal.SIGINT, exithandler)
rfdevice = RFDevice(args.gpio)
rfdevice.enable_rx()
timestamp = None
logging.info("Listening for codes on GPIO " + str(args.gpio))
code_of_interest = '384450'
#
while True:
if rfdevice.rx_code_timestamp != timestamp:
timestamp = rfdevice.rx_code_timestamp
print(str(rfdevice.rx_code))
if str(rfdevice.rx_code) == code_of_interest:
post_image()
time.sleep(1) # prevent registering multiple times
time.sleep(0.01)
rfdevice.cleanup()
任何解决这个问题的帮助都将是非常好的。我试着跟着一个门铃走,当它的射频码在家庭助理身上运行时,它就会工作。我可以从HA手动运行脚本,当我在上面运行脚本时它会出错
我已经想了一个星期了,但是我想不出来。我的代码不是很好,但我可以做基本的python
谢谢。有一个具有您想要的功能的
因此,请使用:
而不是datetime.now()
如果希望保持原样,请将导入语句从import datetime
更改为from datetime import datetime
尝试更改此选项:
def post_image():
print('Taking photo')
camera.capture('image.jpg')
file_name = 'image_' + str(datetime.now()) + '.jpg'
camera.capture(file_name) # time-stamped image
with open('image.jpg', "rb") as imageFile:
myFile = imageFile.read()
data = bytearray(myFile)
client.publish('dev/camera', data, mqttQos, mqttRetained) #
client.publish('dev/test', 'Capture!') # to trigger an automation later
print(file_name + 'image published')
为此:
def post_image():
print('Taking photo')
camera.capture('image.jpg')
file_name = 'image_' + str(datetime.datetime.now()) + '.jpg'
camera.capture(file_name) # time-stamped image
with open('image.jpg', "rb") as imageFile:
myFile = imageFile.read()
data = bytearray(myFile)
client.publish('dev/camera', data, mqttQos, mqttRetained) #
client.publish('dev/test', 'Capture!') # to trigger an automation later
print(file_name + 'image published')
您应该使用:
datetime.datetime.now()
,或者从datetime
模块导入datetime
对象