HENNGE / arsenic

Async WebDriver implementation for asyncio and asyncio-compatible frameworks
Other
351 stars 54 forks source link

ImportError: cannot import name 'get_session' from partially initialized module 'arsenic' (most likely due to a circular import) #82

Closed Luciferianism closed 4 years ago

Luciferianism commented 4 years ago

Code:

import asyncio
import sys
from arsenic import get_session, browsers, services

if sys.platform.startswith('win'):
    GeckoDriver = 'C:\\Users\\Zandrio\\AppData\\Local\\Programs\\Python\\Python38\\geckodriver.exe'
else:
    GeckoDriver = './geckodriver.exe'    

service = services.Geckodriver(binary=GeckoDriver)
browser = browsers.Firefox(firefoxOptions={ 'args': ['-headless']} )    

async def browser_object():   
  async with get_session(service, browser) as session:
       await session.get('https://google.com/')
       await asyncio.sleep(10)

def main():
 loop = asyncio.get_event_loop()
 loop.run_until_complete(browser_object())

if __name__ == "__main__":
    main()

Terminal Output:

PS C:\Users\Zandrio> & C:/Users/Zandrio/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/Zandrio/Documents/Advanced Project/arsenic.py" Traceback (most recent call last): File "c:/Users/Zandrio/Documents/Advanced Project/arsenic.py", line 3, in <module> from arsenic import get_session, browsers, services File "c:\Users\Zandrio\Documents\Advanced Project\arsenic.py", line 3, in <module> from arsenic import get_session, browsers, services ImportError: cannot import name 'get_session' from partially initialized module 'arsenic' (most likely due to a circular import) (c:\Users\Zandrio\Documents\Advanced Project\arsenic.py)

konstunn commented 4 years ago

I think all you need to do is just to rename your own source code file from arsenic.py to anything else, for example, to try_arsenic.py or main.py, whatever. Cause there is a conflict / collapse between your own module and arsenic module, you know what I mean?

Luciferianism commented 4 years ago

I think all you need to do is just to rename your own source code file from arsenic.py to anything else, for example, to try_arsenic.py or main.py, whatever. Cause there is a conflict / collapse between your own module and arsenic module, you know what I mean?

Thanks, i renamed the file and it's working now. Just a question does this library has browser close function, i cannot find that in the documentations of arsenic. I also tried pyppeteer2, it has the await browser.close() function.

konstunn commented 4 years ago

According to my experience using arsenic if you don't have your browser running then when you do async with get_session(..), then your browser is going to be launched, do what you tell him to do by your arsenic code and then exit.

TL;DR: get_session opens browser and then closes it.

Luciferianism commented 4 years ago

According to my experience using arsenic if you don't have your browser running then when you do async with get_session(..), then your browser is going to be launched, do what you tell him to do by your arsenic code and then exit.

TL;DR: get_session opens browser and then closes it.

Is requests-html with asyncio a better substitute of arsenic ?, if i am just interested in loading the javascript to get the full outerhtml, i dont have to click any button or submit forms on a website.

konstunn commented 4 years ago

According to your problem you really don't need arsenic. Requests-html better suits your needs, I think.

h1n054ur commented 3 years ago

Hi, I am getting the same error, is there a workaround? I just pip installed it.

konstunn commented 3 years ago

Hi @h1n054ur . Make sure your module name does not clash with some others. Review your modules structure to make sure you do not have circular imports. Rework your structure if have circular imports.