Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import worldometer error #7

Open
viplazylmht opened this issue Apr 9, 2021 · 1 comment
Open

import worldometer error #7

viplazylmht opened this issue Apr 9, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@viplazylmht
Copy link

I have found a problem that is, I can't import worldometer to notebooks.

Env: anaconda3, WSL
Version: Python 3.8.3, worldometer-1.0.1

The full error response is:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~/anaconda3/lib/python3.8/site-packages/worldometer/core.py in _get_html(self, url)
    233             self.__r = session.get(url, timeout=self.__timeout)
--> 234             self.__r.html.render(timeout=self.__timeout)
    235 

~/anaconda3/lib/python3.8/site-packages/requests_html.py in render(self, retries, script, wait, scrolldown, sleep, reload, timeout, keep_page)
    585 
--> 586         self.browser = self.session.browser  # Automatically create a event loop and browser
    587         content = None

~/anaconda3/lib/python3.8/site-packages/requests_html.py in browser(self)
    728             if self.loop.is_running():
--> 729                 raise RuntimeError("Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.")
    730             self._browser = self.loop.run_until_complete(super().browser)

RuntimeError: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-3-c4b22f2d5ddf> in <module>
----> 1 import worldometer

~/anaconda3/lib/python3.8/site-packages/worldometer/__init__.py in <module>
     82 
     83 from .core import Worldometer
---> 84 from .api import *

~/anaconda3/lib/python3.8/site-packages/worldometer/api.py in <module>
    106 
    107 
--> 108 __w = Worldometer()
    109 
    110 

~/anaconda3/lib/python3.8/site-packages/worldometer/core.py in __init__(self, timeout)
    179         self.__timeout = timeout
    180 
--> 181         self._metrics = self.collect_metrics()
    182 
    183     def __str__(self):

~/anaconda3/lib/python3.8/site-packages/worldometer/core.py in collect_metrics(self)
    298 
    299         if self.__r is None:
--> 300             html = self._get_html(url=URL)
    301         else:
    302             html = self.__r.html.raw_html

~/anaconda3/lib/python3.8/site-packages/worldometer/core.py in _get_html(self, url)
    237 
    238         except Exception as err:
--> 239             raise Exception(err)
    240 
    241     @staticmethod

Exception: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.

I thinks this is one of a limitation of WSL hence hope anyone find out the solution soon.

@matheusfelipeog
Copy link
Owner

matheusfelipeog commented Apr 13, 2021

Hi @viplazylmht, Thanks for the information. ;)

This is a problem of event loop.

As stated in psf/requests-html#140 (comment)

This is due to jupyter use an event loop under the hood and request-html calls loop.run_until_complete which rise that exception when the loop is already running...

There are others discussions about this in:

ref 1: jupyter/notebook#3397
ref 2: psf/requests-html#140
ref 3: psf/requests-html#294
ref 4: https://medium.com/@vyshali.enukonda/how-to-get-around-runtimeerror-this-event-loop-is-already-running-3f26f67e762e
ref 5: https://stackoverflow.com/questions/46827007/runtimeerror-this-event-loop-is-already-running-in-python

I tested all possible solutions contained in the discussions, but unsuccessful. 😢

I recommend that you use worldometer outside the jupyter notebook, until there is a solution to this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants