Error AttributeError: module 'aiohttp' has no attribute 'ClientSession'

Question:

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)
Asked By: Drake Ramoray

||

Answers:

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.

Answered By: ewong
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.