Python Forum
SyntaxError: invalid syntax ??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: invalid syntax ??
#1
Hello ,
starnge problem
I have a working python code that print me canbus data
today I enter the code to a new machine and I get this error (just copied the code)
def ReadCanBusData():
    bus = can.interface.Bus(channel='can0', bustype='socketcan')
    while True:
        try:
            message = bus.recv(10)
        except Exception as e:
            print('error in CanBus!')
        else:
            if message is None:
                print('TimeOut!')
            else:
                TS = datetime.datetime.now()
                pid_temp = message.arbitration_id
                pid_int = int(pid_temp)
                data_byte = message.data
                try:
                    data_temp = [f"{byte:02x}" for byte in data_byte]
                except Exception as e:
                    print(e)
                Data = ' '.join(data_temp)
                PID = f"{pid_int:08X}"
                PID = PID.upper()
                print(str(TS) + ": " + PID + ": " + Data)
when I try to run it I get :
 python3 ReadCanbus.py
  File "ReadCanbus.py", line 39
    data_temp = [f"{byte:02x}" for byte in data_byte]
                             ^
SyntaxError: invalid syntax
and also this (when I disable line 39,40):
 python3 ReadCanbus.py
  File "ReadCanbus.py", line 41
    PID = f"{pid_int:08X}"
                         ^
SyntaxError: invalid syntax
again - it's the same file running on other machine

waht is this , and what is wrong?
Thanks ,
Reply
#2
f-strings appeared in Python 3.6. The other machine may be running an older version of Python.
Reply
#3
Run this code.
import sys

print(sys.version)

data_byte = b'123'
data_temp = [f"{byte:02x}" for byte in data_byte]
print(data_temp)
Output:
3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] ['31', '32', '33']
So as mention need a version newer that Python 3.6(f-string was new there),now you see which version before get SyntaxError.
Reply
#4
ok
I thought it from 2.6 and UP

this is what I have :
3.5.3 (default, Apr  5 2021, 09:00:41)
so just use apt-get update on the PI an it will update the python ?
or I need to do something else?

Thanks,
Reply
#5
How To Install the Latest Python Version on Raspberry Pi?

I use pyenv for all OS that is not Windows,my tutorial.
How to Setup Raspberry Pi OS: Bullseye like a Python Pro
Reply
#6
Thank you
Reply
#7
Your OS may also have packages for python3.8 or python3.9 which you could also install.
Reply
#8
I did everything it saidin the guide
https://raspberrytips.com/install-latest-python-raspberry-pi/
but when it still show me the oldest version
pi@localhost:~ $ python3 --version
Python 3.5.3
Reply
#9
korenron Wrote:https://raspberrytips.com/install-latest...pberry-pi/
Quote:Always run a Python script with the exact version you want to use, for example:
python3.9 myscript.py
Read further how to change the link.
Quote:Link the version your want to use instead:
sudo ln -s /usr/local/bin/python3.9 python
Reply
#10
this is what I got :
 sudo ln -s /usr/local/bin/python3.9 python3
sudo: unable to resolve host localhost: No such file or directory
this is an old PI3 , maybe I can't make it work ?

maybe I can do something else in the code so it will work with version 3.5?
just this part ?
  PID = f"{pid_int:08X}"
I have found I can do this :
PID = str('{:08x}'.format(pid_temp))
and it's working

but how can I do it for this part:
data_temp = [f"{byte:02x}" for byte in data_byte]
I have try :
for byte in data_byte:                 
                    test = str('{byte:02x}'.format(byte))
                    print(test)
 
but I'm wrong....


Thanks,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,204 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  Invalid syntax with an f-string Mark17 7 7,839 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,912 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,175 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,267 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,766 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,289 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 2,339 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 2,394 Dec-10-2020, 04:26 PM
Last Post: deanhystad
  Error on nested loop : Invalid syntax dvazquezgu 3 3,239 Nov-25-2020, 10:04 AM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020