matheusfelipeog / worldometer

Get live, population, geography, projected, and historical data from around the world 🌍

Home Page:https://worldometer.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

import worldometer error

viplazylmht opened this issue · comments

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.

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.