1

I have a python program that use kivy and create a gui. I tried to make the program an executable using pyinstaller but when I try to run the exe file it opens and closes the app immediately. I tried to run the exe using the cmd but it just opened it in another window and closed it immediately, I also tried using the -c flag when using pyinstaller on the .py file but there was no change. what can I do to solve it?

Edit: I tried adding a print("hello") in the first line and then exit right after it, and then I created an exe again, but the result hasn't changed, the file opened and closed immediately. Therefore I tried running the app with with the cmd with start /B forcing it to run in the cmd, I saw it got the following error

C:\Users\YShay\Desktop\New folder\dist>Traceback (most recent call last):
File "C:\Users\YShay\AppData\Local\Programs\Python\Python38\Lib\sitepackages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 13, in <module>
import pkg_resources as res
File "c:\users\yshay\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pkg_resources\__init__.py", line 86, in <module>
ModuleNotFoundError: No module named 'pkg_resources.py2_warn'
[19380] Failed to execute script pyi_rth_pkgres 

Does it mean it pyinstaller failed to import one of the modules? If so how could I import it or make it accessible for the application?

Those are all the imports of my program:

import socket
import sys
import threading
import psutil
import socket
import subprocess
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from functools import partial
SH22
  • 21
  • 1
  • 5
  • 1
    If you run the exe from a command prompt, do you get any console output? Try adding some print statements in main() before the gui starts, maybe even make it exit just before the gui loop starts, see what you get? A common problem when trying to use pyinstaller is that your code can’t find initialization files because they aren’t in the usual location - could that be your problem? – DisappointedByUnaccountableMod Mar 03 '20 at 22:32
  • Did you read e.g. https://kivy.org/doc/stable/api-kivy.tools.packaging.pyinstaller_hooks.html and https://stackoverflow.com/questions/37696206/how-to-get-an-windows-executable-from-my-kivy-app-pyinstaller and possibly many other results from goggling something like __pyinstaller kivy__ – DisappointedByUnaccountableMod Mar 04 '20 at 17:20

1 Answers1

0

First, if you are unable to install Kivy on Python 3.8 you can install it by typying:

python -m pip install docutils pygments pypiwin32 kivy_deps.sdl2==0.1.* kivy_deps.glew==0.1.*
python -m pip install kivy_deps.gstreamer==0.1.*
python -m pip install kivy_deps.angle==0.1.*
python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew --extra-index-url https://kivy.org/downloads/packages/simple
pip install kivy[base] kivy_examples --pre --extra-index-url https://kivy.org/downloads/simple/

Then when you will pack the Kivy GUI app with Pyinstaller and you will see an error related to:

Failed to execute script pyi_rth_pkgres 

Just uninstall the setuptools and install version 44.0.0 (link)

pip uninstall setuptools

pip install setuptools==44.0.0

Also, the packaging is a little tricky, because it is done it two steps official guide on how to pack Kivy with Pyinstaller:

navigate to your Kivy examples directory. For me it looks like:

C:\Users\YOUR-WINDOWS-USERNAME\AppData\Local\Programs\Python\Python38-32\share\kivy-examples\demo\touchtracer

create a directory for the packaged application and cd to it. Then in this directory type:

python -m PyInstaller --name touchtracer C:\Users\YOUR-WINDOWS-USERNAME\AppData\Local\Programs\Python\Python38-32\share\kivy-examples\demo\touchtracer\main.py

You will see that the *.spec file is created. Open it in the text editor.

On the beginning of the file, just after

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

Press enter and type

from kivy_deps import sdl2, glew

Make sure that your COLLECT will look like this (add two Tree's):

coll = COLLECT(exe, Tree('examples-path\\demo\\touchtracer\\'),
           a.binaries,
           a.zipfiles,
           a.datas,
           *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
           strip=False,
           upx=True,
           name='touchtracer')

Now back to your CMD. Build the app using edited spec file: python -m PyInstaller touchtracer.spec

Your compiled exe file will be in

C:\Users\YOUR-WINDOWS-USERNAME\AppData\Local\Programs\Python\Python38-32\share\kivy-examples\demo\touchtracer\paczka\dist\touchtracer

For the packaging purpose, you can check now does exe is working on a different computer.