Can't create a .exe with python kivy on windows (Pyinstaller)
Ok so I figured it out.
FIRST OF ALL, if you are having this problem, you will have to first add the following line to your code:
def reset():
import kivy.core.window as window
from kivy.base import EventLoop
if not EventLoop.event_listeners:
from kivy.cache import Cache
window.Window = window.core_select_lib('window', window.window_impl, True)
Cache.print_usage()
for cat in Cache._categories:
Cache._objects[cat] = {}
if __name__ == '__main__':
reset()
'your app name here'().run()
That will prevent the app from not opening (like when it looks like its loading but nothing opens).
AFTER THAT, you wanna build using the spec like this:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
from kivy_deps import sdl2, glew, gstreamer
a = Analysis(['C:\\Users\\Artur\\PycharmProjects\\gameficacao\\Gameficacao.py'],
pathex=['C:\\Users\\Artur\\Desktop\\Trabalho\\Gameficacao'],
binaries=[],
datas=[('C:/Users/Artur/PycharmProjects/gameficacao/*.kv', '.'), ('C:/Users/Artur/PycharmProjects/gameficacao/img/*.png', './img'),('C:/Users/Artur/PycharmProjects/gameficacao/font/*.ttf', './font'),('C:/Users/Artur/PycharmProjects/gameficacao/som/*.mp3', './som')],
hiddenimports=['pkg_resources.py2_warn','win32timezone','six','packaging','packaging.version','webbrowser','kivy','enchant'],
hookspath=[],
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='Gameficacao',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins + gstreamer.dep_bins)],
strip=False,
upx=True,
upx_exclude=[],
name='Gameficacao')
Now, some things are VERY IMPORTANT here and the app probably won't run without it, and those are:
- The line "from kivy_deps import sdl2, glew, gstreamer", right on the top
- The filling of the data part of the Analysis, here you will have to indicate the path of every file you will use on your app, including the kivy file. Use mine as a example.
- The hiddenimports line, here you will point out every import that is relevant for your app and that PyInstaller is not able to do. For starters, you will probably want to put the 'pkg_resources.py2_warn', since PyInstaller can't import it properly and you will need it.
- The line "*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins + gstreamer.dep_bins)]," will be a complement from the imports done on the beggining of the code.
That solved the problem for me.
Creating an executable kivy app and exe installer
Making an executable from a complex script like yours may become quite frustrating because of its dependencies. But I'm giving you a brief guide about what you need to follow to achieve your goal.
Create your
main.spec
file with console-mode enabled to see the exact error message for the app. (make sure to remove--noconsole
from PyInstaller command or setconsole=True
in spec file). Also use--no-upx
in the build command to remove compression from output file (this helps with omitting some DLLs which may cause issues).You need to make sure that every external module you used can pack correctly. I don't think you get any problem with either Kivy or Tweepy. But if you get any missing import error, try to check the solution for each one by searching the pattern
[module] pyinstaller
.Your app has external resources like images, files, etc., which must be added to the packed executable and load properly. I wrote an answer about this here.
If you want a standalone executable, you need to use
-F
with PyInstaller command, which is more robust than using an installer to gather files in one directory mode.
Windows kivy app successfully builds using PyInstaller, but doesn't work
well, I actually figured it out myself. Though I don't think that my solution is in any way elegant and it might cause some other problems in the future, but i just edited the pyi_rth_pkgutil.py from PyInstaller package directory (how it is now, how it used to be) and reran the pyinstaller package creation script. It just worked after that
Convert Kivymd code to .exe (Windows executable)
Your .spec
file seems overly complicated. Here is a main.spec
file that works for me and your main.py
:
# -*- mode: python ; coding: utf-8 -*-
from kivy_deps import sdl2, glew
block_cipher = None
a = Analysis(['main.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,
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )
And just run pyinstaller main.spec
. This should create a single file executable main.exe
.
Python to EXE generated through Pyinstaller not running for kivy based python
try this :
from kivy_deps import sdl2, glew
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['gridLayout.py'],
pathex=['C:\\Users\\sj3kc0\\Desktop\\kivy'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
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='gridLayout',
debug=True,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True)
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
strip=False,
upx=True,
upx_exclude=[],
name='gridLayout')
# by setting the debug and console to True in the exe this will display the console
# when you run your exe so you can see the log and the errors if any happened if
# your exe work without any errors you can make them False to disable the console
and then you can run it like following:pyinstaller pyinstaller.spec
Kivy One-Folder packaging with PyInstaller looks for _MEIxxxxxx path?
It sounds like there may be some confusion with .spec
files. According to the docs:
When you create a spec file, most command options are encoded in the
spec file. When you build from a spec file, those options cannot be
changed. If they are given on the command line they are ignored and
replaced by the options in the spec file.Only the following command-line options have an effect when building
from a spec file:--upx-dir
--distpath
--workpath
--noconfirm
--ascii
--clean
The spec
file that you posted is for creating a onedir
executable, and adding the --onedir
option has no effect.
I suggest removing the build
and dist
folders, along with the .spec
file. Then create a new spec
file using pyi-makespec --onedir
.Then edit that newly created .spec
file as needed. Then run pyinstaller <app-name>.spec
with no command line options. This should create a dist
folder with nothing in it other than another folder with the name of your main python file. Inside that folder you should find all the files needed to run your app, including a python39.dll
file and an exe
file.
Generate a single exe using pyinstaller for a kivy app
Running Pyinstaller
with a .spec
file argument makes it ignore almost any options that you provide on the command line. I would recommend doing a pyi-makespec --onefile touch.py
to get a one file starting .spec
file. Then edit the touch.spec
file to add the sdl
stuff. Just add the same line (*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)]
) in the exe
portion just after the a.datas
line (just like you have it in the coll
section). Then just run python -m PyInstaller touch.spec
.
Related Topics
Difference Between Python'S Generators and Iterators
Find the Similarity Metric Between Two Strings
How to Get the Last Day of the Month
How to Call a Parent Class'S Method from a Child Class in Python
Pandas Column of Lists, Create a Row For Each List Element
Proxies With Python 'Requests' Module
Typeerror: 'Str' Does Not Support the Buffer Interface
Sieve of Eratosthenes - Finding Primes Python
Boolean Operators VS Bitwise Operators
What Does -≫ Mean in Python Function Definitions
Fitting Empirical Distribution to Theoretical Ones With Scipy (Python)
Selecting With Complex Criteria from Pandas.Dataframe
How to Connect to Flask Local Server
How to Deploy a Python Dash Application on an Internal Company Server
Faster Way to Find Large Files with Python
Syntax Error Near Unexpected Token '(' Python Script