Search code examples
pythonpython-3.xkivypyinstallertrace

Endless trace messages when converting python kivy program to exe file with pyinstaller


So i am trying to convert an example kivy program into an exe standalone file from this documentation page, just as a bleak example to investigate why my pyinstaller and kivy do not work: https://kivy.org/doc/stable/tutorials/firstwidget.html

A .kv file is not needed for this particular implementation.

The code is in the file main2.py:

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line


class MyPaintWidget(Widget):

    def on_touch_down(self, touch):
        color = (random(), 1, 1)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 30.
            Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]


class MyPaintApp(App):

    def build(self):
        parent = Widget()
        self.painter = MyPaintWidget()
        clearbtn = Button(text='Clear')
        clearbtn.bind(on_release=self.clear_canvas)
        parent.add_widget(self.painter)
        parent.add_widget(clearbtn)
        return parent

    def clear_canvas(self, obj):
        self.painter.canvas.clear()


if __name__ == '__main__':
    MyPaintApp().run()

i used the command: (tried with and without the "--onefile")

pyinstaller --onefile -w main2.py

Everything goes good until pyinstaller outputs this endless list of trace messages that don't stop and keep on printing in the terminal:

349 TRACE: _safe_import_hook 'sys' Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) None 0
[TRACE  ] [_safe_import_hook 'sys' Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) None 0
8353 TRACE: _import_hook 'sys' Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) 0 
[TRACE  ] [_import_hook 'sys' Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) 0  
8359 TRACE: determine_parent Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy'])
[TRACE  ] [determine_parent Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy'])
8362 TRACE: determine_parent -> Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy'])
[TRACE  ] [determine_parent -> Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy'])
8363 TRACE: find_head_package Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) 'sys' 0
[TRACE  ] [find_head_package Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) 'sys' 0
8367 TRACE: safe_import_module 'sys' 'sys' None
[TRACE  ] safe_import_module 'sys' 'sys' None
8369 TRACE: safe_import_module -> BuiltinModule('sys',)
[TRACE  ] safe_import_module -> BuiltinModule('sys',)
8372 TRACE: find_head_package -> (BuiltinModule('sys',), '')
[TRACE  ] find_head_package -> (BuiltinModule('sys',), '')
8375 TRACE: load_tail BuiltinModule('sys',) ''
[TRACE  ] load_tail BuiltinModule('sys',) ''
8376 TRACE: load_tail -> BuiltinModule('sys',)
[TRACE  ] load_tail -> BuiltinModule('sys',)
8377 TRACE: createReference Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) BuiltinModule('sys',) DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False)
[TRACE  ] [createReference Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) BuiltinModule('sys',) DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False)
8379 TRACE: _safe_import_hook 'shutil' Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) None 0
[TRACE  ] [_safe_import_hook 'shutil' Package('kivy', 'c]\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) None 0
8381 TRACE: _import_hook 'shutil' Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) Package('kivy', 'c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy\\__init__.py', ['c:\\users\\coderalpha\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\kivy']) 0

The process generates this main2.spec file though:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main2.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='main2',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='main2',
)

and when i run the "exe file" that has been created, it gives me this recursion error as an error message (without opening any application window):

Failed to execute script 'main2' due to unhandled exception: maximum recursion depth exceeded while calling a Python object

Traceback (most recent call last):
  File "logging\__init__.py", line 1084, in emit
AttributeError: 'NoneType' object has no attribute 'write'

During handling of the above exception, another exception occurred:

... <same exception repeated many times> ...

I also tried to follow the .spec file changes given here: https://www.youtube.com/watch?v=NEko7jWYKiE

...without any luck, it gives me the same traces and error message.

Additional information: PyInstaller: version 5.11.0 Python: version 3.8.3

Any answers to how i can convert it to an exe file and get rid of these errors will be appreciated.


Solution

  • I also had the same problem. Downgrading the pyinstaller version fixed it. My advice would be to change your pyinstaller version to 5.6.2. First run

    pyinstaller --onefile main2.py
    

    Then edit the created spec file like this.

    # -*- mode: python ; coding: utf-8 -*-
    
    from kivy_deps import sdl2, glew
    from kivymd import hooks_path as kivymd_hooks_path
    
    block_cipher = None
    app_name = 'Your App Name'
    win_icon = '.\icon.ico'
    
    
    a = Analysis(
        ['main.py'],
        pathex=[],
        binaries=[],
        hiddenimports=[],
        datas =[('./main.kv', '.'), ('./*.ico', '.')],
        hookspath=[kivymd_hooks_path],
        hooksconfig={},
        runtime_hooks=[],
        excludes=[],
        win_no_prefer_redirects=False,
        win_private_assemblies=False,
        cipher=block_cipher,
        noarchive=False,
    )
    pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
    
    exe = EXE(
        pyz,
        a.scripts,
        a.binaries,
        a.zipfiles,
        a.datas,
        *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
        name=app_name,
        debug=False,
        bootloader_ignore_signals=False,
        strip=False,
        upx=True,
        upx_exclude=[],
        runtime_tmpdir=None,
        console=False,
        icon=win_icon,
        disable_windowed_traceback=False,
        argv_emulation=False,
        target_arch=None,
        codesign_identity=None,
        entitlements_file=None,
    )
    

    It's not necessary to edit it this way, but I'm adding it here anyway. Finally run

    pyinstaller main.spec
    

    It will be extracted as YourApplicationName.exe to the folder named dist.