【问题标题】:Keep getting syntax error for: async def on_ready():不断收到以下语法错误:async def on_ready():
【发布时间】:2021-05-09 07:56:59
【问题描述】:

我正在尝试使用此代码制作一个不和谐的机器人,在 Visual Code Studio 中一切正常,但我在终端中收到此错误。我知道I get a syntax error when I import discord in the cmd but not in vs code,但它并没有解决问题,我也提前道歉,我是一个python初学者,几乎不知道如何运行代码:

from discord.ext import commands
from utils.logger import Logger

logger = Logger().logger
glados_cores = ["cogs.angry_core"]
bot = commands.Bot(command_prefix="!")
bot.logger = logger


@bot.event
async def on_ready():
    logger.info("---------------bot-ready---------------")
    logger.info("Hello and, again, welcome to the Aperture Science computer-aided enrichment center.")


if __name__ == "__main__":
    for extension in glados_cores:
        try:
            bot.load_extension(extension)
        except Exception as e:
            exc = f"{type(e).__name__}: {e}"
            logger.info(f"{exc} Failed to load extension {extension}")

bot.run(TOKEN)

当我尝试运行代码时,我不断收到此语法错误:

Aleksandars-16-MacBook-Pro:Genetic-Lifeform-and-Disk-Operating-System-master aleksandarvasilic$ python main.py
  File "main.py", line 11
    async def on_ready():
        ^
SyntaxError: invalid syntax

【问题讨论】:

  • 这能回答你的问题吗? Python [Invalid syntax] with async def
  • @RandomDavis 不,我已经看过那个帖子了。
  • 那么你在哪个版本的 Python 上运行它?
  • @RandomDavis 3.9.1
  • 其实2.7.16

标签: python discord.py


【解决方案1】:

如果您没有 python 3.5+,就会发生这种情况。如果您在 cmd 中遇到语法错误,可能是因为您的路径上安装了 python

【讨论】:

  • 我有 python 3.9.1
  • 但是你确定这就是你用来运行机器人的东西吗?
  • 等等。你说的对。我刚刚检查过,我有 python 版本 2.7.16 并且没有安装 pip。我想我可能安装了多个 python 版本。前段时间我遇到了python和pip的问题,所以我通过删除它们的文件夹手动卸载了python和pip。我的系统可能对所有事情都很困惑,我该如何解决这个问题?
  • 我有 2.7.16 而不是 3.9.1
  • @AleksandarVasilic 你可以使用python3 而不是python 来运行脚本。或者给命令起别名,就像描述的here
猜你喜欢
  • 2018-08-01
  • 2021-01-17
  • 1970-01-01
  • 2020-03-07
  • 2022-07-21
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多