Discord Python Raspbian 运行 脚本时错误语法无效
Discord Python Raspbian Error invalid syntax when running script
这是我第一次在这个平台上写作。使用 lavalink 托管 discord 音乐机器人时,我的 RPi 出现错误。目前,我的问题是一行代码中存在无效语法错误。奇怪的是,当我在 Windows 10 上使用 VSCode 时,我没有收到此错误。它在 Win 10 中也 运行 完美。我尝试安装所需的软件包,试图搜索可能是什么问题。我没有找到任何结果。我想知道我这里的主要问题是什么。
主要源代码:
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(' ', '%20')})"
控制台错误:
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
更何况我的树莓派是运行 Raspbian x64.
:=
语法(即赋值表达式)是在 Python 3.8 中引入的(例如,参见 https://docs.python.org/3/whatsnew/3.8.html),根据您分享的回溯,你 运行宁 Python 3.7.
如果您希望您的脚本在此旧版本上 运行,则必须重写它以不使用此语法。例如,您可以从 if
的条件中提取赋值:
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(' ', '%20')})"
这是我第一次在这个平台上写作。使用 lavalink 托管 discord 音乐机器人时,我的 RPi 出现错误。目前,我的问题是一行代码中存在无效语法错误。奇怪的是,当我在 Windows 10 上使用 VSCode 时,我没有收到此错误。它在 Win 10 中也 运行 完美。我尝试安装所需的软件包,试图搜索可能是什么问题。我没有找到任何结果。我想知道我这里的主要问题是什么。
主要源代码:
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(' ', '%20')})"
控制台错误:
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
更何况我的树莓派是运行 Raspbian x64.
:=
语法(即赋值表达式)是在 Python 3.8 中引入的(例如,参见 https://docs.python.org/3/whatsnew/3.8.html),根据您分享的回溯,你 运行宁 Python 3.7.
如果您希望您的脚本在此旧版本上 运行,则必须重写它以不使用此语法。例如,您可以从 if
的条件中提取赋值:
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(' ', '%20')})"