Home > Back-end >  Discord Python Raspbian Error invalid syntax when running script
Discord Python Raspbian Error invalid syntax when running script

Time:09-16

This is my first time writing in this platform. I got an error on my RPi when hosting a discord music bot using lavalink. Currently, my problem is that there's a invalid syntax error on a line of code. The weird part is that I don't get this error when I use VSCode on Windows 10. It also ran perfect in Win 10. I've tried to install the required packages, tried to search up what could be the problem. I found no results. I was wondering what could be my main issue over here.

Main src code:

if (currentTrackData := player.fetch("currentTrackData")) != None:
            embed.set_thumbnail(url=currentTrackData["thumbnail"]["genius"])
            embed.description  = f"\n[LYRICS]({currentTrackData['links']['genius']}) | [ARTIST](https://genius.com/artists/{currentTrackData['author'].replace(' ', ' ')})"

Console Error:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 860, in get_code
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/pi/SpotBot/cogs/Music.py", line 168
    if (currentTrackData := player.fetch("currentTrackData")) != None:
                         ^
SyntaxError: invalid syntax

Not to mention my RPi is running Raspbian x64.

CodePudding user response:

The := syntax (i.e., the assignment expression) was introduced in Python 3.8 (see, e.g., https://docs.python.org/3/whatsnew/3.8.html), and according to the traceback you've shared, you're running Python 3.7.

If you want your script to run on this older version, you'll have to rewrite it to not use this syntax. For example, you could extract the assignment out of the if's condition:

currentTrackData = player.fetch("currentTrackData")
if currentTrackData != None:
    embed.set_thumbnail(url=currentTrackData["thumbnail"]["genius"])
    embed.description  = f"\n[LYRICS]({currentTrackData['links']['genius']}) | [ARTIST](https://genius.com/artists/{currentTrackData['author'].replace(' ', ' ')})"
  • Related