6

When I ran the code below with @asyncio.coroutine decorator on Python 3.11.0:

import asyncio

@asyncio.coroutine # Here
def test():
    print("Test")

asyncio.run(test())

I got the error below:

AttributeError: module 'asyncio' has no attribute 'coroutine'. Did you mean: 'coroutines'?

I find @asyncio.coroutine decorator is used for some code as far as I googled.

So, how can I solve this error?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129

1 Answers1

8

Generator-based Coroutines which contains @asyncio.coroutine decorator is removed since Python 3.11 so asyncio module doesn't have @asyncio.coroutine decorator as the error says:

Note: Support for generator-based coroutines is deprecated and is removed in Python 3.11.

So instead, you need to use async keyword before def as shown below:

import asyncio

# Here
async def test():
    print("Test")

asyncio.run(test()) # Test

Then, you can solve the error:

Test
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129