SyntaxError: invalid syntax on python script

1k Views Asked by At

I'm not familiar with Python, but I made the following script with searching on Google.

The script pulls out data from a device and injects it in Domoticz. When I run the script on my Windows machine, every minute it loops correctly and injects the data into Domoitcz. When I run the same script on my Raspberry Pi, it returns a SyntaxError: invalid syntax (line 155) on the except Exception line...

Even if I put a # before that line it raises an error.

#!/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)

Why does it work on my Windows machine correctly and not on the Raspberry Pi?

1

There are 1 best solutions below

0
On BEST ANSWER

The line raising the error is a print statement which is using an f-string (e.g. f"{variable_name}"). F-strings were introduced in Python 3.6, so my guess is that your raspberry pi is using a version earlier than that, and your windows machine isn't.

Try checking what version of python is running on your raspberry pi (python --version from a terminal). If it's anything earlier than 3.6, the f-strings will cause you syntax errors everywhere in the code.

The only other issue I can think of is you might actually be running python 2.7 with the default python command (I can't remember off the top of my head if rasbian does this like some distros). If you get 2.7 with the version check above, try using python3 and check that version. You will need to be running that script with at least 3.6 for that script to execute.

Incidentally, you have another f-string on line 156, which might explain why if you commented out 155, it raises the exact same syntax error :)