带有 async def 的 Python [无效语法]

Python [Invalid syntax] with async def(带有 async def 的 Python [无效语法])

本文介绍了带有 async def 的 Python [无效语法]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 编写 discord 机器人,我遇到了这个机器人并将其拼凑在一起.

I am trying write discord bots using Python, I have come across and threw together this bot.

import discord
import asyncio
import random

client = discord.Client()
inEmail = input("Email:")
inPassword = input("Passwd:")

async def background_loop():
    await client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("************")
        messages = ["Hello!", "How are you doing?", "Testing!!"]
        await client.send_message(channel, random.choice(messages))
        await asyncio.sleep(120)

client.loop.create_task(background_loop())
client.run(inEmail, inPassword)

然而,当我尝试运行它时,我收到了一个 SyntaxError:

Yet when I tried to run it, I received a SyntaxError:

File "1.py", line 7
  async def background_loop():
     ^
SyntaxError: invalid syntax

这是为什么呢?我之前测试时从未收到过.

Why is that? I have never received that before when I tested it.

推荐答案

2021 年更新答案(discord.py 1.x - 2.x):

discord.py 目前支持 Python 3.5 及更高版本.如果您收到 SyntaxError,则表示您使用的是旧版本的 Python,并且不被 discord.py 支持.

Updated answer for 2021 (discord.py 1.x - 2.x):

discord.py currently supports Python 3.5 and up. If you receive a SyntaxError, it means you're using an older version of Python and isn't supported by discord.py.

首先,安装更高版本的 Python(在此消息中首选 3.8.x),然后运行 ​​python3.8 bot.pypy -3.8 bot.py(适用于 Windows).注意:安装时不要忘记选择Add Python to PATH".

First, install a later version of Python (3.8.x is preferred as of this message), and run python3.8 bot.py or py -3.8 bot.py (for Windows) when running your bot. Note: when installing, don't forget to choose "Add Python to PATH".

在 v3.3 中将异步请求引入 Python,如果如果您运行的是 v3.3 之前的 Python(包括 v2.X),则必须安装更新版本的 Python.

Asynchronous requests were introduced to Python in v3.3, if you're running Python prior to v3.3 (including v2.X), you'll have to install a newer version of Python.

在您运行 Python 3.3 时:asyncio 不是标准库的一部分,你需要从 pypi 手动安装它:

Only if you are running Python 3.3: asyncio is not part of the stdlib, you'll need to install it manually from pypi:

pip install asyncio

asyncawait 关键字仅对 Python 3.5 或更新版本有效.如果您使用的是 Python 3.3 或 3.4,则需要对代码进行以下更改:

The async and await keywords are only valid for Python 3.5 or newer. If you're using Python 3.3 or 3.4, you will need to make the following changes to your code:

  1. 使用 @asyncio.coroutine 装饰器代替 async 语句:
  1. Use the @asyncio.coroutine decorator instead of the async statement:

async def func():
    pass

# replace to:

@asyncio.coroutine
def func():
    pass

  1. 使用 yield from 而不是 await:
  1. Use yield from instead of await:

await coroutine() 

# replace to:

yield from coroutine()

这是您的函数需要更改为的示例(如果您使用的是 3.3-3.4):

Here is an example of what your function need to change into (if you're on 3.3-3.4):

import asyncio

@asyncio.coroutine 
def background_loop():
    yield from client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("************")
        messages = ["Hello!", "How are you doing?", "Testing!!"]
        yield from client.send_message(channel, random.choice(messages))
        yield from asyncio.sleep(120)

较新版本的 Python 3 仍然支持上述语法,但如果不需要支持 Python 3.3-3.4,建议使用 awaitasync.您可以参考这个 文档,这里有一个简短的片段:

The aforementioned syntax is still supported in newer versions of Python 3, but it is recommended to use await and async if there's no need to support for Python 3.3-3.4. You can refer back to this documentation, here's a short snippet:

async def 类型的协程是在 Python 3.5 中添加的,并且是如果不需要支持较旧的 Python 版本,建议使用.

The async def type of coroutine was added in Python 3.5, and is recommended if there is no need to support older Python versions.


旁白:

discord.py 目前支持 3.4.2-3.6.6,(不支持 3.3-3.4.1,截至 2019 年 1 月为 3.7).


Aside:

discord.py currently supports 3.4.2-3.6.6, (It does not support 3.3-3.4.1, 3.7 as of January 2019).

对于使用 discord.py 进行开发,我建议使用 discord.py 重写分支:

For developing with discord.py, I suggest using the discord.py rewrite branch:

discord.py-重写支持3.5.3-3.7.

discord.py-rewrite supports 3.5.3-3.7.

这篇关于带有 async def 的 Python [无效语法]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 async def 的 Python [无效语法]

基础教程推荐