Whenever I run code which requires aiohttp, I get the error below:
JavaScript
x
14
14
1
Traceback (most recent call last):
2
File "C:UsersHP.atomasync8.py", line 1, in <module>
3
from aiohttp import web
4
File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttp__init__.py", line 6, in <module>
5
from .client import * # noqa
6
File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttpclient.py", line 15, in <module>
7
from . import connector as connector_mod
8
File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttpconnector.py", line 13, in <module>
9
from . import hdrs, helpers
10
File "C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesaiohttphelpers.py", line 30
11
ensure_future = asyncio.async
12
^
13
SyntaxError: invalid syntax
14
Examples of codes I’ve tried to run from https://docs.aiohttp.org/en/stable/ include:
JavaScript
1
17
17
1
import aiohttp
2
import asyncio
3
4
async def main():
5
6
async with aiohttp.ClientSession() as session:
7
async with session.get('http://python.org') as response:
8
9
print("Status:", response.status)
10
print("Content-type:", response.headers['content-type'])
11
12
html = await response.text()
13
print("Body:", html[:15], "...")
14
15
loop = asyncio.get_event_loop()
16
loop.run_until_complete(main())
17
and
JavaScript
1
15
15
1
from aiohttp import web
2
3
async def handle(request):
4
name = request.match_info.get('name', "Anonymous")
5
text = "Hello, " + name
6
return web.Response(text=text)
7
8
9
app = web.Application()
10
app.add_routes([web.get('/', handle),
11
web.get('/{name}', handle)])
12
13
if __name__ == '__main__':
14
web.run_app(app)
15
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.