Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 960
Kai - Kazuya Ito Avatar asked Jan 27 '26 06:01

Kai - Kazuya Ito


1 Answers

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
like image 78
Kai - Kazuya Ito Avatar answered Jan 29 '26 21:01

Kai - Kazuya Ito