Ask coding questions

← Back to all posts
'await' outside function error
abriani434 (1)

I've encountered a very frustrating delay. I'm using the python environment and I'm having an error saying "SyntaxError: 'await' outside function". Does anyone know how to fix this?

Comments
hotnewtop
Coder100 (18915)

All await keywords must be in an async function, like this:

async def func():
  await ...
InvisibleOne (3224)

To use await, it needs to be inside of an asynchronous function, this will give you an error:

def hello(message):
  await message.channel.send("Hello!")

but this won't:

async def hello(message):
  await message.channel.send("Hello!")
VulcanWM (2904)

When you make the function in which the await is in, define it as async def functionname() instead of def functionname()

abriani434 (1)

@Coder100 @InvisibleOne @VulcanWM thanks :) i'll try these and see if they work

abriani434 (1)

ok - well, the error doesn't show up anymore, but now the bot won't respond to the ay!newr and ay!delr commands. (newr stands for 'new roast' and delr stands for 'delete roast')

Does anyone have any fixes for this?


VulcanWM (2904)

That’s because you haven’t made a function for them @abriani434

abriani434 (1)

@VulcanWM oh i see .-. im very very new at this so uh i dont know that much - how do i make a function?

Coder100 (18915)

The reason why you have the error is because you unindented, so it was outside of an async function.