Home > Enterprise >  Error AttributeError: module 'aiohttp' has no attribute 'ClientSession'
Error AttributeError: module 'aiohttp' has no attribute 'ClientSession'

Time:09-02

Error AttributeError: module 'aiohttp' has no attribute 'ClientSession', But ClientSession exists in module, idk how to solve it. i tried everthing someone help

import aiohttp
import asyncio
import json
import time

async def get_page (session,url): 
  async with session.get(url) as r:
   return await r.text()


async def get_all(session,urls) :
    tasks = []
    for url in urls:
        task = asyncio.create_task(get_page(session,url) )
        tasks.append(task)
    results = await asyncio.gather(*tasks)
    return results

async def main (urls) :
    async with aiohttp.ClientSession() as session : # Error here
        data = await get_all(session,urls)
        return data

def parse(results):
    for html in results:   
        data = json.loads(html)       
    return

if __name__ == '__main__':
    urls = ['https://www.google.com']
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    results = asyncio.run(main(urls))
    parse(results)

CodePudding user response:

The problem is you've named the script aiohttp.py which interferes with python's ability to use the aiohttp module.

Rename that file to aiohttp_custom.py (or something else) and your problem should be gone.

  • Related