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

Created on 17 Apr 2016  ·  7Comments  ·  Source: kivy/kivy

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()

Most helpful comment

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()

All 7 comments

Any update on this issue or resolution by anyone?

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()

maybe the workaround should be part of kivy.support.install_twisted_reactor?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

We need a way to override the reactor selection OR even allow a way to completely ignore the twisted hook. Is there a way currently to do any of that ?

The same problem occurs when using PyInstaller qt5 and Twisted with the custom qt5reactor.
rnixx' workarround solves the problem.
Thank you

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

from PySide2.QtWidgets import QApplication
app = QApplication(sys.argv) 
import qt5reactor
qt5reactor.install()

PS: Sorry for posting a qt related solution in a kivy related thread. Yet this ReactorAlreadyInstalledError seems to be a general problem of the twisted-hook when using different reactors

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hansent picture hansent  ·  3Comments

phelantomas picture phelantomas  ·  5Comments

damienflament picture damienflament  ·  3Comments

sbrother picture sbrother  ·  4Comments

knappador picture knappador  ·  6Comments