Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 400
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 8
Kenil Vasani
Asked: December 11, 20202020-12-11T20:34:41+00:00 2020-12-11T20:34:41+00:00In: Python

Python [Invalid syntax] with async def

  • 8

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)

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.

async-awaitdiscorddiscord.pypythonsyntax
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Rohit Patel

      Rohit Patel

      • 0 Questions
      • 98 Answers
      • 0 Best Answers
      • 0 Points
      View Profile
      Best Answer
      Rohit Patel
      2020-12-11T20:34:06+00:00Added an answer on December 11, 2020 at 8:34 pm

      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.


      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
      

      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. Use the @asyncio.coroutine decorator instead of the async statement:
      async def func():
          pass
      
      # replace to:
      
      @asyncio.coroutine
      def func():
          pass
      
      1. Use yield from instead of await:
      await coroutine() 
      
      # replace to:
      
      yield from coroutine()
      

      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)
      

      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:

      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.


      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).

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

      discord.py-rewrite supports 3.5.3-3.7.

      • 6
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      FATAL EXCEPTION: Firebase-Messaging-Intent-Handle — java.lang.NoClassDefFoundError

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com