Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

python - Error whenever I run code that requires aiohttp library

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.

question from:https://stackoverflow.com/questions/65857763/error-whenever-i-run-code-that-requires-aiohttp-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...