RunTimeError in Twint : This event loop is already running - python

I use a jupyter notebook and run twint.
Code
c = twint.Config()
c.Username = 'twitter'
c.Limit = 20
twint.run.Followers(c)
The above commands produce a runtime error relating to (I believe) the code attempting to create 2 asynchronous event loops.
Traceback as follows:
RuntimeError Traceback (most recent call last)
<ipython-input-4-4713cc05fb59> in <module>()
----> 1 twint.run.Favorites(c)
~/coding/tools/twint/twint/run.py in Favorites(config)
119 def Favorites(config):
120 config.Favorites = True
--> 121 run(config)
122
123 def Followers(config):
~/coding/tools/twint/twint/run.py in run(config)
115
116 def run(config):
--> 117 get_event_loop().run_until_complete(Twint(config).main())
118
119 def Favorites(config):
~/.pyenv/versions/3.6.5/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
453 future.add_done_callback(_run_until_complete_cb)
454 try:
--> 455 self.run_forever()
456 except:
457 if new_task and future.done() and not future.cancelled():
~/.pyenv/versions/3.6.5/lib/python3.6/asyncio/base_events.py in run_forever(self)
407 self._check_closed()
408 if self.is_running():
--> 409 raise RuntimeError('This event loop is already running')
410 if events._get_running_loop() is not None:
411 raise RuntimeError(
RuntimeError: This event loop is already running

I have found a solution for Jupyter notebooks
using the nest_async
Simply do
pip install nest_asyncio
And add these lines.
import nest_asyncio
nest_asyncio.apply()

Related

KafkaAdminClient() got error "KafkaConnectionError: KafkaConnectionError: socket disconnected"?

The following python code got error when creating an instance of KafkaAdminClient.
from kafka.admin import KafkaAdminClient, NewTopic
admin_client = KafkaAdminClient(
bootstrap_servers=bootstrap_servers,
client_id='test',
security_protocol="SSL"
)
got the following error:
---------------------------------------------------------------------------
KafkaConnectionError Traceback (most recent call last)
<ipython-input-10-edd53455dcb0> in <module>()
4 admin_client = KafkaAdminClient(
5 bootstrap_servers=bootstrap_servers,
----> 6 client_id='test', security_protocol="SSL"
7 )
8
/apps/external/4/anaconda3/lib/python3.6/site-packages/kafka/admin/client.py in __init__(self, **configs)
216
217 self._closed = False
--> 218 self._refresh_controller_id()
219 log.debug("KafkaAdminClient started.")
220
/apps/external/4/anaconda3/lib/python3.6/site-packages/kafka/admin/client.py in _refresh_controller_id(self)
271 future = self._send_request_to_node(self._client.least_loaded_node(), request)
272
--> 273 self._wait_for_futures([future])
274
275 response = future.value
/apps/external/4/anaconda3/lib/python3.6/site-packages/kafka/admin/client.py in _wait_for_futures(self, futures)
1340
1341 if future.failed():
-> 1342 raise future.exception # pylint: disable-msg=raising-bad-type
KafkaConnectionError: KafkaConnectionError: socket disconnected
However, the following code runs without any error.
from kafka import KafkaConsumer
consumer = KafkaConsumer(
group_id='test',
bootstrap_servers=bootstrap_servers,
security_protocol="SSL")
print(consumer.topics())

Tensorflow hub issue: Can't load embedding model after last update

This code has been working until last tf_hub update. I think, the problem is in the tensorflow_text module, that I haven't installed. But when I try to execute "pip install tensorflow_text==2.3.0" command (copied from the official tf_hub page) it throws back the error. I also tried to install it manually from github repo, but the package is still not available. Thanks.
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
G:\Anaconda\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in get_tensor(self, tensor_str)
69 return CheckpointReader.CheckpointReader_GetTensor(
---> 70 self, compat.as_bytes(tensor_str))
71 # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the
RuntimeError:
During handling of the above exception, another exception occurred:
OpError Traceback (most recent call last)
<ipython-input-8-c716b77a9bc1> in <module>
----> 1 embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
G:\Anaconda\lib\site-packages\tensorflow_hub\module_v2.py in load(handle, tags, options)
112 module_path, tags=tags, options=options)
113 else:
--> 114 obj = tf_v1.saved_model.load_v2(module_path, tags=tags)
115 obj._is_hub_module_v1 = is_hub_module_v1 # pylint: disable=protected-access
116 return obj
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in load(export_dir, tags, options)
601 ValueError: If `tags` don't match a MetaGraph in the SavedModel.
602 """
--> 603 return load_internal(export_dir, tags, options)
604
605
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in load_internal(export_dir, tags, options, loader_cls)
631 try:
632 loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
--> 633 ckpt_options)
634 except errors.NotFoundError as err:
635 raise FileNotFoundError(
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in __init__(self, object_graph_proto, saved_model_proto, export_dir, ckpt_options)
129
130 self._load_all()
--> 131 self._restore_checkpoint()
132
133 for node in self._nodes:
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in _restore_checkpoint(self)
328 self._checkpoint_options).expect_partial()
329 else:
--> 330 load_status = saver.restore(variables_path, self._checkpoint_options)
331 load_status.assert_existing_objects_matched()
332 checkpoint = load_status._checkpoint
G:\Anaconda\lib\site-packages\tensorflow\python\training\tracking\util.py in restore(self, save_path, options)
1280 dtype_map = reader.get_variable_to_dtype_map()
1281 try:
-> 1282 object_graph_string = reader.get_tensor(base.OBJECT_GRAPH_PROTO_KEY)
1283 except errors_impl.NotFoundError:
1284 # The object graph proto does not exist in this checkpoint. Try the
G:\Anaconda\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in get_tensor(self, tensor_str)
72 # issue with throwing python exceptions from C++.
73 except RuntimeError as e:
---> 74 error_translator(e)
75
76
G:\Anaconda\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in error_translator(e)
46 raise errors_impl.InternalError(None, None, error_message)
47 else:
---> 48 raise errors_impl.OpError(None, None, error_message, errors_impl.UNKNOWN)
49
50
OpError:
C:\Users\usr>pip install tensorflow_text v==2.3.0
ERROR: Could not find a version that satisfies the requirement tensorflow_text (from versions: none)
ERROR: No matching distribution found for tensorflow_text
This code is working fine in Tensorflow 2.7 in Anaconda jupyter notebook. Please specify the Tensorflow version you are using while running this code.
!pip install tensorflow-hub
import tensorflow_hub as hub
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
There is no need to install the tensorflow_text to run this piece of code. However I have found there is typo error in your traceback error while installing tensorflow_text.
It should be as below:
!pip install tensorflow_text==2.3.0
Please check this link to access the Universal Sentence Encoder on TF-Hub.

Getting error "NameError: name 'base' is not defined" while trying to run a Deep Q Network in Google Collab

Does anyone know how to resolve this issue? I'm using Google Collab to try run my code but I cant seem to fix this issue.
Note: I have an Environment setup in Pycharm where this issue doesn't appear and the code runs (just very laggy due to intense training).
Currently running on Tensorflow 2.0 and Python 3.7
NameError Traceback (most recent call last)
<ipython-input-5-14174aa69df8> in <module>()
666
667 # Find Initial State
--> 668 cur_state = env.reset()
669 dqn_agent = DQN(env, cur_state.shape)
670 dqn_agent.save_model('models/deepq.h5')
4 frames
<ipython-input-5-14174aa69df8> in reset(self)
303 self.car = Car(self.world, *self.track[0][1:4])
304
--> 305 return self.step(None)[0]
306
307 def step(self, action):
<ipython-input-5-14174aa69df8> in step(self, action)
319 self.t += 1.0 / FPS
320
--> 321 self.state = self.render("state_pixels")
322
323 step_reward = 0
<ipython-input-5-14174aa69df8> in render(self, mode)
340 assert mode in ['human', 'state_pixels', 'rgb_array']
341 if self.viewer is None:
--> 342 from gym.envs.classic_control import rendering
343 self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H)
344 self.score_label = pyglet.text.Label('0000', font_size=36,
/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/rendering.py in <module>()
25
26 try:
---> 27 from pyglet.gl import *
28 except ImportError as e:
29 raise ImportError('''
/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py in <module>()
223 elif compat_platform == 'darwin':
224 from .cocoa import CocoaConfig as Config
--> 225 del base # noqa: F821
226
227 # XXX remove
NameError: name 'base' is not defined
Link to the code - https://github.com/eoinmca/Final_Year_Project/blob/master/google_collab_version.py
If anyone tries to reproduce this error you need to ensure collab is running tf2 not tf1
Prerequisites to set up collab
!pip install box2d-py
!pip install gym[Box_2D]
print(tf.__version__)
# Run next lines if not on tf2
!pip uninstall tensorflow
!pip install tensorflow==2.0.0

why kivy gives error in starting of the code?

I downloaded kivy successfully.
When I was trying to start the kivy window with this command that I found at the original kivy basics web site:
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text='Hello world')
if __name__ == '__main__':
MyApp().run()
It gives an error:
ArgumentError Traceback (most recent call last)
D:\Anaconda\lib\site-packages\kivy\core\window\window_sdl2.py in mainloop(self)
746 try:
--> 747 self._mainloop()
748 except BaseException as inst:
D:\Anaconda\lib\site-packages\kivy\core\window\window_sdl2.py in _mainloop(self)
478 def _mainloop(self):
--> 479 EventLoop.idle()
480
D:\Anaconda\lib\site-packages\kivy\base.py in idle(self)
361 Logger.error('Base: Application will leave')
--> 362 self.exit()
363 return False
D:\Anaconda\lib\site-packages\kivy\base.py in exit(self)
374 '''Close the main loop and close the window.'''
--> 375 self.close()
376 if self.window:
D:\Anaconda\lib\site-packages\kivy\base.py in close(self)
171 self.quit = True
--> 172 self.stop()
173 self.status = 'closed'
D:\Anaconda\lib\site-packages\kivy\base.py in stop(self)
183 for provider in reversed(self.input_providers[:]):
--> 184 provider.stop()
185 if provider in self.input_providers_autoremove:
D:\Anaconda\lib\site-packages\kivy\input\providers\wm_pen.py in stop(self)
110 self.pen = None
--> 111 SetWindowLong_WndProc_wrapper(self.hwnd, self.old_windProc)
112
D:\Anaconda\lib\site-packages\kivy\input\providers\wm_common.py in _closure(hWnd, wndProc)
121 def _closure(hWnd, wndProc):
--> 122 oldAddr = func(hWnd, GWL_WNDPROC, cast(wndProc, c_void_p).value)
123 return cast(c_void_p(oldAddr), WNDPROC)
ArgumentError: argument 3: <class 'TypeError'>: wrong type
During handling of the above exception, another exception occurred:
ArgumentError Traceback (most recent call last)
D:\Anaconda\lib\site-packages\kivy\base.py in runTouchApp(widget, slave)
503 else:
--> 504 EventLoop.window.mainloop()
505 finally:
D:\Anaconda\lib\site-packages\kivy\core\window\window_sdl2.py in mainloop(self)
751
Please help me why doesn't it works?
Here is the example of working: https://kivy.org/doc/stable/guide/basic.html

Sage hangs when calling Mathematica

I recently installed Sage 6.3 on my Fedora 21 machine. I'm using version 6.3, which is slightly outdated, because it is the most recent thing available in yum's repositories. I also installed Mathematica on the same computer in the hope of being able to call it from within Sage.
Mathematica's terminal interface using the math command works, which according to this reference page should be all that I need. However, when I tell Sage to use mathematica either from the Sage command line or the notebook, Sage hangs. Here's a sample of my interaction with the terminal (the traceback is horrible, as is frequently the case in Sage):
sage: mathematica('hello world')
^CInterrupting Mathematica...
^C---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-1-9208bb19d841> in <module>()
----> 1 mathematica('hello world')
/usr/lib64/python2.7/site-packages/sage/interfaces/interface.pyc in __call__(self, x, name)
197
198 if isinstance(x, basestring):
--> 199 return cls(self, x, name=name)
200 try:
201 return self._coerce_from_special_method(x)
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in __init__(self, parent, value, is_name, name)
1310 else:
1311 try:
-> 1312 self._name = parent._create(value, name=name)
1313 # Convert ValueError and RuntimeError to TypeError for
1314 # coercion to work properly.
/usr/lib64/python2.7/site-packages/sage/interfaces/interface.pyc in _create(self, value, name)
387 def _create(self, value, name=None):
388 name = self._next_var_name() if name is None else name
--> 389 self.set(name, value)
390 return name
391
/usr/lib64/python2.7/site-packages/sage/interfaces/mathematica.pyc in set(self, var, value)
508 cmd = '%s=%s;'%(var,value)
509 #out = self.eval(cmd)
--> 510 out = self._eval_line(cmd, allow_use_file=True)
511 if len(out) > 8:
512 raise TypeError("Error executing code in Mathematica\nCODE:\n\t%s\nMathematica ERROR:\n\t%s"%(cmd, out))
/usr/lib64/python2.7/site-packages/sage/interfaces/mathematica.pyc in _eval_line(self, line, allow_use_file, wait_for_prompt, restart_if_needed)
535 def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, restart_if_needed=False):
536 s = Expect._eval_line(self, line,
--> 537 allow_use_file=allow_use_file, wait_for_prompt=wait_for_prompt)
538 return str(s).strip('\n')
539
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in _eval_line(self, line, allow_use_file, wait_for_prompt, restart_if_needed)
890 out = ''
891 except KeyboardInterrupt:
--> 892 self._keyboard_interrupt()
893 raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self)
894 if self._terminal_echo:
/usr/lib64/python2.7/site-packages/sage/interfaces/mathematica.pyc in _keyboard_interrupt(self)
420 e = self._expect
421 e.sendline(chr(3)) # send ctrl-c
--> 422 e.expect('Interrupt> ')
423 e.sendline("a") # a -- abort
424 e.expect(self._prompt)
/usr/lib64/sagemath/site-packages/pexpect.pyc in expect(self, pattern, timeout, searchwindowsize)
914 """
915 compiled_pattern_list = self.compile_pattern_list(pattern)
--> 916 return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
917
918 def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
/usr/lib64/sagemath/site-packages/pexpect.pyc in expect_list(self, pattern_list, timeout, searchwindowsize)
965 raise TIMEOUT ('Timeout exceeded in expect_list().')
966 # Still have time left, so read more data
--> 967 c = self.read_nonblocking (self.maxread, timeout)
968 incoming = incoming + c
969 if timeout is not None:
/usr/lib64/sagemath/site-packages/pexpect.pyc in read_nonblocking(self, size, timeout)
546 raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.')
547
--> 548 r, w, e = select.select([self.child_fd], [], [], timeout)
549 if not r:
550 if not self.isalive():
/usr/lib64/python2.7/site-packages/sage/ext/c_lib.so in sage.ext.c_lib.sage_python_check_interrupt (build/cythonized/sage/ext/c_lib.c:1683)()
/usr/lib64/python2.7/site-packages/sage/ext/c_lib.so in sage.ext.c_lib.sig_raise_exception (build/cythonized/sage/ext/c_lib.c:769)()
KeyboardInterrupt:
sage:
The equivalent in Mathematica's own interface works just fine:
Mathematica 10.1.0 for Linux x86 (64-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= hello world
Out[1]= hello world
In[2]:=
Am I doing something wrong, or is this a bug in Sage? Would downloading and building the latest version (6.7) manually fix the problem?
EDIT:
I am using Mathematica 10.1.0. Could it be that be my problem, i.e. the older version of Sage doesn't know how to handle the newer version of Mathematica?
According to Trac 16703, this problem should be fixed in the latest Sage. I don't personally have a copy of Mathematica to test this on.

Categories

Resources