Python3.12使用r6sapi报错:module 'asyncio' has no attribute 'coroutine'
问题描述
首次使用Asyncio调用r6sapi编写Python脚本时,触发以下错误:
AttributeError: module 'asyncio' has no attribute 'coroutine'. Did you mean: 'coroutines'?
完整报错栈如下:
Traceback (most recent call last): File "c:\Users\cincy\OneDrive\Documents\PythonSiegeAPI\r6api.py", line 2, in <module> import r6sapi as api File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\r6sapi\__init__.py", line 17, in <module> from .auth import * File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\r6sapi\auth.py", line 11, in <module> import aiohttp File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\__init__.py", line 6, in <module> from .client import * # noqa ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\client.py", line 15, in <module> from . import connector as connector_mod File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\connector.py", line 12, in <module> from . import hdrs, helpers File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\helpers.py", line 26, in <module> from .abc import AbstractAccessLogger File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\abc.py", line 10, in <module> class AbstractRouter(ABC): File "C:\Users\cincy\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\abc.py", line 31, in AbstractRouter @asyncio.coroutine # pragma: no branch ^^^^^^^^^^^^^^^^^
使用的代码示例:
import asyncio import r6sapi as api async def run(): auth = api.Auth("email", "password") player = await auth.get_player("PLAYER_NAME", api.Platforms.UPLAY) operator = await player.get_operator("sledge") print(operator.kills) await auth.close() asyncio.get_event_loop().run_until_complete(run())
解决办法
原因分析
Python 3.12正式移除了asyncio.coroutine装饰器(这是旧版生成器协程的写法,Python 3.7+已推荐使用async def定义协程),而当前安装的aiohttp版本过于老旧,仍在调用这个已被移除的API,导致报错。
具体修复步骤
升级aiohttp到兼容Python3.12的版本
执行以下命令:pip uninstall aiohttp pip install --upgrade aiohttp升级后aiohttp会使用新版协程写法,不再依赖
asyncio.coroutine。优化代码的协程运行方式
原代码中asyncio.get_event_loop().run_until_complete(run())是旧版写法,Python3.7+推荐用更简洁的asyncio.run()替代,修改后的代码:import asyncio import r6sapi as api async def run(): auth = api.Auth("email", "password") player = await auth.get_player("PLAYER_NAME", api.Platforms.UPLAY) operator = await player.get_operator("sledge") print(operator.kills) await auth.close() asyncio.run(run())若r6sapi本身不兼容Python3.12
升级aiohttp后仍报错的话,说明r6sapi库未适配Python3.12,此时需要降级Python版本到3.11及以下,或等待r6sapi发布兼容新版本的更新。
内容的提问来源于stack exchange,提问作者JaredCusick
相关产品推荐
相关产品推荐

