Whenever I run code which requires aiohttp, I get the error below:
Traceback (most recent call last): File "C:UsersHP.atomasync8.py", line 1, in <module> from aiohttp import web File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttp__init__.py", line 6, in <module> from .client import * # noqa File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttpclient.py", line 15, in <module> from . import connector as connector_mod File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttpconnector.py", line 13, in <module> from . import hdrs, helpers File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttphelpers.py", line 30 ensure_future = asyncio.async ^ SyntaxError: invalid syntax
Examples of codes I’ve tried to run from https://docs.aiohttp.org/en/stable/ include:
import aiohttp import asyncio async def main(): async with aiohttp.ClientSession() as session: async with session.get('http://python.org') as response: print("Status:", response.status) print("Content-type:", response.headers['content-type']) html = await response.text() print("Body:", html[:15], "...") loop = asyncio.get_event_loop() loop.run_until_complete(main())
and
from aiohttp import web async def handle(request): name = request.match_info.get('name', "Anonymous") text = "Hello, " + name return web.Response(text=text) app = web.Application() app.add_routes([web.get('/', handle), web.get('/{name}', handle)]) if __name__ == '__main__': web.run_app(app)
I’ve tried several other examples but they all produce the same error. What could be causing this?. I’m using python 3.8.6 and the latest version of aiohttp
Update: It seems the error is caused by importing aiohttp. I get the error just by typing ‘import aiohttp’ on cmd.
Advertisement
Answer
It seems that
pip install aiohttp
installs a version incompatible with my current python version. pip install aiohttp==3.5.4
solved the issue for me. Thank you @edoput for pointing that out.