"AttributeError: module 'asyncio' has no attribute 'coroutine'." in Python 3.11.0
Asked Answered
O

1

14

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?

Olivette answered 7/11, 2022 at 10:23 Comment(0)
O
14

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
Olivette answered 7/11, 2022 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.