SyntaxError: invalid syntax on python script

SyntaxError: invalid syntax on python script

我不熟悉 Python,但我通过在 Google 上搜索制作了以下脚本。

脚本从设备中提取数据并将其注入 Domoticz。 当我 运行 我的 Windows 机器上的脚本时,它每分钟都会正确循环并将数据注入 Domoitcz。当我 运行 在我的 Raspberry Pi 上使用相同的脚本时,它 returns 一个 SyntaxError: invalid syntax (line 155) on the except Exception line...

即使我在该行之前放一个 # 也会引发错误。

#!/usr/bin/env python3

from APSystemsECUR import APSystemsECUR
import time
import asyncio
import urllib.request
import urllib.parse
import urllib
from pprint import pprint


ecu_ip = "192.168.178.xx"
sleep = 60

url = 'http://192.168.178.xx:8080/json.htm?'
puntcomma = '\u003B'

loop = asyncio.get_event_loop()
ecu = APSystemsECUR(ecu_ip)

while True:
        try:
                data = loop.run_until_complete(ecu.async_query_ecu())
                #pprint(data)

                today_energy_kwh = str(data.get('today_energy')*1000)
                print('Today energy [kWh]: ' + today_energy_kwh)
                getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 212, 'svalue': (today_energy_kwh)}
                webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                #print(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                
                getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 213, 'svalue': data.get('current_power')}
                webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')     
                #print(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                
                getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 195, 'svalue': data.get('timestamp')}
                webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                #print(url + urllib.parse.urlencode(getVars))
#inverter values
                inverters = data.get('inverters')
#count number of inverters
                Inverter_qty = len(data.get('inverters'))
                print('Inverter_cnt: ' + str(Inverter_qty))
# loop trough all inverters and get the data
                for i in range(Inverter_qty):
                 Inverter = list(inverters.keys())[i]
                 print('InverterId: ' + Inverter)
                 InverterOnline = data['inverters'][Inverter]['online']
                 print('Online: ' + str(InverterOnline))
                 InverterTemperature = data['inverters'][Inverter]['temperature']
                 print('Temperature: ' + str(InverterTemperature))
                 nPower = len(data['inverters'][Inverter]['power'])
                 for x in range(nPower):
                  power = data['inverters'][Inverter]['power'][x]
                  print('Power inverter ' + str(i + 1) + ' panel ' + str(x + 1) + ': ' + str(power) + ' W')

#upload values to Domoticz voor inverter 1
                  if (i == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 173, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 174, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 174, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload values to Domoticz voor inverter 2
                  if (i == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 179, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 180, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 180, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload values to Domoticz voor inverter 3
                  if (i == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 185, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 186, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 186, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload values to Domoticz voor inverter 4
                  if (i == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 191, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 192, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 192, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload power values to Domoticz voor inverter 1
                  if (i == 0) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 196, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 0) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 197, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 0) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 198, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 0) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 199, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')

#upload power values to Domoticz voor inverter 2
                  if (i == 1) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 200, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 1) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 201, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 1) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 202, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 1) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 203, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')

#upload power values to Domoticz voor inverter 3
                  if (i == 2) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 204, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 2) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 205, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 2) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 206, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 2) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 207, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')

#upload power values to Domoticz voor inverter 4
                  if (i == 3) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 208, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 3) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 209, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 3) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 210, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 3) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 211, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')


        except Exception as err:
                print(f"[ERROR]", {err})

        #print(f"Sleeping for {sleep} sec")
        time.sleep(sleep)

为什么它在我的 Windows 机器上运行正常,而在 Raspberry Pi 上运行不正常?

引发错误的行是 print 语句,它使用了 f 字符串(例如 f"{variable_name}")。 F 弦是在 Python 3.6 中引入的,所以我的猜测是您的 raspberry pi 使用的是早于该版本的版本,而您的 windows 机器不是。

尝试在您的 raspberry pi(python --version 从终端)上检查 python 的 运行 是什么版本。如果它是早于 3.6 的版本,f-strings 将导致您在代码中到处出现语法错误。

我能想到的唯一另一个问题是你可能实际上是 运行 python 2.7 使用默认的 python 命令(我不记得了如果 rasbian 像某些发行版那样这样做)。如果您通过上述版本检查获得 2.7,请尝试使用 python3 并检查该版本。您将需要 运行 该脚本至少 3.6 才能执行该脚本。

顺便说一下,您在第 156 行还有另一个 f 字符串,这可以解释为什么如果您注释掉 155,它会引发完全相同的语法错误:)