question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using twisted, kivy and pyinstaller raise error.ReactorAlreadyInstalledError("reactor already installed")

See original GitHub issue

spec_file

c:\Users\sony\Dropbox\notepad_app\2_Paint\build_windows>cat noteapp.spec
# -*- mode: python -*-

block_cipher = None


a = Analysis(['..\\__main__.py'],
             pathex=['c:\\Users\\sony\\Dropbox\\notepad_app\\2_Paint\\build_windows'],
             binaries=None,
             datas=None,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='noteapp',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='noteapp')

running executable

The first line prints all modules loaded till now. To check if twisted reactor gets loaded before from kivy.support import install_twisted_reactor

c:\Users\sony\Dropbox\notepad_app\2_Paint\build_windows\dist\noteapp>cmd /K noteapp.exe
[<module 'pyimod03_importers' from 'c:\python27\lib\site-packages\PyInstaller-3.2.dev0+g2f86ac6-py2.7.egg\PyInstaller\loader\pyimod03_importers.py'>, <module 'tempfile' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\tempfile.pyc'>, <module 'sys' (built-in)>, <module 'atexit' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\atexit.pyc'>, <module 'ctypes' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\ctypes\__init__.pyc'>, <module 'encodings' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\encodings\__init__.pyc'>, <module 'win32com' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\win32com\__init__.pyc'>, <module 'shutil' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\shutil.pyc'>, <module 'os' from 'c:\Users\sony\Dropbox\NOTEPA~1\2_Paint\BUILD_~1\dist\noteapp\os.pyc'>]
[INFO              ] [Logger      ] Record log in c:/Users/sony\.kivy\logs\kivy_16-04-17_2.txt
[INFO              ] [Kivy        ] v1.9.2-dev0
[INFO              ] [Python      ] v2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)]
 Traceback (most recent call last):
   File "c:\Users\sony\Dropbox\notepad_app\2_Paint\__main__.py", line 7, in <module>
     install_twisted_reactor() # twisted eventloop with kivy on the same reactor
   File "c:\python27\lib\site-packages\kivy\support.py", line 176, in install_twisted_reactor
     _threadedselect.install()
   File "c:\Python27\lib\site-packages\twisted\internet\_threadedselect.py", line 354, in install
     installReactor(reactor)
   File "c:\Python27\lib\site-packages\twisted\internet\main.py", line 32, in installReactor
     raise error.ReactorAlreadyInstalledError("reactor already installed")
 twisted.internet.error.ReactorAlreadyInstalledError: reactor already installed
Failed to execute script __main__

main.py

#Written according to https://kivy.org/docs/guide/other-frameworks.html
#works fine when using python __main__.py
import sys
modulenames = set(sys.modules)&set(globals())
allmodules = [sys.modules[name] for name in modulenames]
print allmodules
#reactor import (compulsory first import)
from kivy.support import install_twisted_reactor
install_twisted_reactor() # twisted eventloop with kivy on the same reactor
#kivy import
from kivy.app import App
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang import Builder
#twisted and other-control imports
from twisted.internet import reactor
#screens and widgets imports
from screens.login_screen import LoginScreen
from canvas.canvas_widget import CanvasWidget,RadioButton
from screens.approve_clients import ApproveClients
#server and behaviour configuration file import
from config import config
try:
    import _cffi_backend
except:
    print "Please install PyInstaller for building. Not necessary for running."
#adding twisted imports
from twisted.internet.defer import DeferredQueue
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import NetstringReceiver
from twisted.internet import defer
from twisted.protocols.basic import NetstringReceiver
from twisted.internet.defer import inlineCallbacks
from twisted.internet.threads import deferToThread
from twisted.internet.protocol import ClientFactory
import os
###############################################
#Screen specified below have majority behaviour specified in .kv
#Other screens are sin ./screens
class ActivitySelectScreen(Screen):
    '''allows user to choose whether to 
            1. create server 
            2. connect to a pre-existing one '''
    def __init__(self,**kwargs):
        super(ActivitySelectScreen, self).__init__(**kwargs)


class PaintApp(Screen):
    '''Screen class of the canvas widget'''
    def __init__(self,**kwargs):
        super(PaintApp, self).__init__(**kwargs)

class AppScreenManager(ScreenManager):
    '''ScreenManager class'''

class notepad_app_v0(App):
    '''Base application class'''
    def build(self):
        self.reactor=reactor
        self.config_obj=config(allow_all=False,debug=False,activate_audio=True)
        return Builder.load_file('.'+os.sep+'kivy'+os.sep+'paint.kv')

if __name__ == '__main__':
    #setting resolution
    Config.set('graphics', 'width', '960')
    Config.set('graphics', 'height', '540')  # 16:9
    #disabling multi-touch and resizing the app
    Config.set('graphics', 'resizable', '0')
    Config.set('input', 'mouse', 'mouse,disable_multitouch')
    notepad_app_v0().run()

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
rnixxcommented, Oct 12, 2016

In the executable created by pyinstaller twisted.internet.reactor seems to be initialized before actual kivy code runs (whyever). As workaround you can do:

import kivy
kivy.require('1.9.1')

# fix for pyinstaller packages app to avoid ReactorAlreadyInstalledError
import sys
if 'twisted.internet.reactor' in sys.modules:
    del sys.modules['twisted.internet.reactor']

# install twisted reactor
from kivy.support import install_twisted_reactor
install_twisted_reactor()
0reactions
titocommented, Mar 11, 2019

Thanks for bringing it back, but it’s not the Kivy fault at all. Check about https://github.com/pyinstaller/pyinstaller/issues/3390 - It’s a generic issue on pyinstaller with their run-time hook that install twisted reactor before you can actually select it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactor already installed - Google Groups
I had error "reactor already installed" after packing mine app with pyinstaller. Is any way for using twisted with kivy and pyinstaller?
Read more >
twisted.internet.error.ReactorAlreadyInstalledError: reactor ...
This class is a thin wrapper that encapsulates some simple helpers to run multiple crawlers, but it won't start or interfere with existing ......
Read more >
reactor already installed [5804] Failed to execute script main
import kivy kivy.require('1.9.1') # fix for pyinstaller packages app to avoid ReactorAlreadyInstalledError import sys if 'twisted.internet.
Read more >
reactor already installed [5804] Failed to execute script main
twisted.internet.error.ReactorAlreadyInstalledError: reactor already installed [5804] Failed to execute script main ; 1. import kivy ; 2. kivy.require('1.9.1') ; 3.
Read more >
Creating packages for OS X — Kivy 1.11.1 documentation
Packaging with PyInstaller is recommended for general use. ... To use Python 3, brew install python3 and replace pip with pip3 in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found