【问题标题】:Error whenever I run code that requires aiohttp library每当我运行需要 aiohttp 库的代码时出错
【发布时间】:2021-01-23 09:49:52
【问题描述】:

每当我运行需要 aiohttp 的代码时,我都会收到以下错误:

Traceback (most recent call last):
  File "C:\Users\HP\.atom\async8.py", line 1, in <module>
    from aiohttp import web
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\__init__.py", line 6, in <module>
    from .client import *  # noqa
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\client.py", line 15, in <module>
    from . import connector as connector_mod
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\connector.py", line 13, in <module>
    from . import hdrs, helpers
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\helpers.py", line 30
    ensure_future = asyncio.async
                            ^
SyntaxError: invalid syntax

我尝试从https://docs.aiohttp.org/en/stable/ 运行的代码示例包括:

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())

    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)

我尝试了其他几个示例,但它们都产生了相同的错误。这可能是什么原因造成的?我正在使用 python 3.8.6 和最新版本的 aiohttp

更新: 该错误似乎是由导入 aiohttp 引起的。我只是通过在 cmd 上输入“import aiohttp”得到错误。

【问题讨论】:

  • 嘿,我在project issue tracker上搜索了一下,看来你根本没有使用最新版本的库!下次如果您也可以将版本与问题一起放入,以便人们现在您使用的是什么!
  • 你是对的。运行 'pip install aiohttp' 然后 'pip list' 显示安装的版本是 2.2.5。但是,运行 'pip install aiohttp==3.5.4' 解决了这个问题。谢谢。

标签: python python-3.x python-asyncio aiohttp


【解决方案1】:

看来 pip install aiohttp 安装的版本与我当前的 python 版本不兼容。 pip install aiohttp==3.5.4 为我解决了这个问题。感谢@edoput 指出这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2022-12-06
    相关资源
    最近更新 更多