Kivy app crashes after packaging with pyinstaller - python

Here are some of the pointers for the issue I've been facing:
Trying to package a example app that comes with Kivy.
I am able to run the app normally via command line [kivy main.py]
When I try to package the app with Pyinstaller, the spec file is generated, and the app can be packaged, but this is what I see in the warning text file: http://pastebin.com/3D2A9ZLG
The app is not able to start after that, and this is the error I see in the console logs: (com.apple.xpc.launchd.oneshot.0x10000028.day2[6584]) Service exited with abnormal code: 1
Kivy Installation
Installed Kivy 1.9.0 on Mac OS X Yosemite using the DMG that came with the download. And ran the MakeSymbols script with sudo user.
Kivy is currently installed in the Applications folder
Pyinstaller 2.0 is being used by downloading the code from their Github repo, and running kivy pyinstaller --windowed --name guide main.py
I'm using Python 2.7
Code:
Here's the spec file for the app I tried creating with the example:
# -*- mode: python -*-
from kivy.tools.packaging.pyinstaller_hooks import install_hooks
install_hooks(globals())
a = Analysis(['/Users/karthik/Desktop/SHRINK/kivy/examples/guide/quickstart/main.py'],
pathex=['/Users/karthik/Desktop/SHRINK/pyinstaller-2.0'],
hiddenimports=[])
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
exclude_binaries=1,
name=os.path.join('build/pyi.darwin/day2', 'day2'),
debug=False,
strip=None,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=None,
upx=False,
name=os.path.join('dist', 'day2'))
app = BUNDLE(coll,
name=os.path.join('dist', 'day2.app'))
Any help would be highly appreciated.

I can see a couple of issues, you're using the os library without importing it which should cause some issues and according to here: (http://kivy.org/docs/guide/packaging-windows.html) you need to include the path in the COLLECT statement so kivy can find everything.
coll = COLLECT( exe, Tree('../kivy27/examples/demo/touchtracer/'),
a.binaries, Tree([f for f in os.environ.get('KIVY_SDL2_PATH', '').split(';') if 'bin' in f][0])
#...
)
Kivy 1.9.0 also uses SDL2 instead of Pygame so you need to link pyinstaller to that as well. In my experience Kivy is quite tempermental working through Pyinstaller so I recommend attempting to follow the link I posted above.

Related

Pyinstaller ModuleNotFoundError on Mac OS

I'm trying to make my python program into an app using pyinstaller. This works flawlessly in Windows, but is not working on Mac OS (Big Sur). I've installed pyinstaller version 5.0dev0.
I started the virtual environment and ran pyinstaller in terminal with the following. (Ultimately, I want to run it as --onefile and without the debug stuff):
(klusterbox) thomasweeks#Thomass-MacBook-Pro kb_install % pyinstaller -w -D -i kb_sub/kb_images/kb_icon1.icns --log-level DEBUG > out.txt klusterbox.py
When I click on the app or open it in terminal, the icon appears in the dock for an instant and disappears. Nothing else happens. When I the Unix Executable File in the dist directory, the, terminal opens and give me this:
Last login: Fri Jan 15 22:24:13 on ttys002
thomasweeks#Thomass-MacBook-Pro ~ % /Users/thomasweeks/klusterbox/kb_install/dist/klusterbox/klusterbox ; exit;
Traceback (most recent call last):
File "klusterbox.py", line 58, in <module>
ModuleNotFoundError: No module named 'PIL'
[24871] Failed to execute script klusterbox
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
This is line 58 in the python program:
# Pillow Library
from PIL import ImageTk, Image # Pillow Library
The Pillow module is installed in the virtual environment:
(klusterbox) thomasweeks#Thomass-MacBook-Pro klusterbox % pip3 list
Package Version
------------------ --------
...
openpyxl 3.0.3
pdfminer.six 20181108
Pillow 8.0.1
pip 20.3.1
...
My .spec file looks like this:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['klusterbox.py'],
pathex=['/Users/thomasweeks/klusterbox/kb_install'],
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='klusterbox',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False , icon='kb_sub/kb_images/kb_icon1.icns')
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='klusterbox')
app = BUNDLE(coll,
name='klusterbox.app',
icon='kb_sub/kb_images/kb_icon1.icns',
bundle_identifier=None)
I've got more raw data if you need to see it.
Edit: I found out later that the below method was made irrelevant by running pyinstaller from inside the virtual environment. After I uninstalled pyinstaller using 'pip uninstall pyinstaller' outside of the virtual environment. I started the virtual environment then installed pyinstaller inside the virtual environment with 'pip install pyinstaller'. After this, pyinstaller found the needed modules. End of edit.
This is a partial answer, where I was able to solve part of the problem. First I went to terminal, navigated to the project folder and made sure the virtual environment was running:
pipenv shell
Next I looked for the missing module which is cited in ModuleNotFoundError: No module named 'PIL'.
pip show Pillow
This gives the following report:
(klusterbox) thomasweeks#Thomass-MacBook-Pro kb_install % pip show Pillow
Name: Pillow
Version: 8.0.1
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark#python-pillow.org
License: HPND
Location: /Users/thomasweeks/.local/share/virtualenvs/klusterbox-K4ySO0c7/lib/python3.7/site-packages
Requires:
Required-by:
I copied the location from the terminal and opened up my klusterbox.spec file. You you it will be .spec in your project folder. I changed the value of pathex in the Analysis tuple in the .spec file.
...
a = Analysis(['klusterbox.py'],
pathex=['/Users/thomasweeks/.local/share/virtualenvs/klusterbox-K4ySO0c7/lib/python3.7/site-packages', '/Users/thomasweeks/klusterbox/kb_install'],
binaries=[],
...
So the pathex value of the Analysis tuple now contains two locations, the location of the project folder and the location of the site packages in the virtual environment. I didn't change anything else in the spec file.
I deleted the old dist, build and pycache folders in Finder.
In terminal I re-ran pyinstaller using the .spec file instead of the .py file. This runs pyinstaller so that it is configured by your revised spec file.
pyinstaller klusterbox.spec
Pyinstaller runs, creates the dist, build and pycache folders. Opening the dist folder there is a klusterbox file (Unix Executable File) and a klusterbox.app file. The klusterbox.app file still will not open, but the Unix Executable File does open and runs although there is a terminal window even though I specified no console in the spec file.

no module named error after pyinstaller python script

What I want: Turn a python file to executable file with all modules and start script.
Problem: Executable file gives no module named signalrcore when execute.
I already have module named 'signalrcore' but when I convert my python script to executable file with pyinstaller it wont work. The error is no module named signalrcore . It only appears with executable file. The python script is work fine. Script work with python2 python myscript.py -> work without any error. But python3 myscript.py -> have same error with executable file.
My python code:
from signalrcore.hub_connection_builder import HubConnectionBuilder
print('TEST')
My .spec file :
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['service.py'],
pathex=['/home/pi/Desktop/agent'],
binaries=[],
datas=[],
hiddenimports=['signalrcore','signalrcore.hub_connection_builder'] ,
hookspath=['/usr/lib/python2.7/dist-packages'],
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='service',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False)
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='service')
This errors pops while compile python code to executable ( sudo pyinstaller service.spec)
56429 INFO: Analyzing hidden import 'signalrcore'
56433 ERROR: Hidden import 'signalrcore' not found
56434 INFO: Analyzing hidden import 'signalrcore.hub_connection_builder'
56438 ERROR: Hidden import 'signalrcore.hub_connection_builder' not found
The problem seems to be, that you installed signalrcore only for the current user, but not for root.
Is there any reason, that you run pyinstaller as root?
This should not be necessary.
Call following command without sudo and look at the output.
python -c "import signalrcore ; print(signalrcore.__file__)"
It will show you where the signalrcore module is installed I assume, that this is a local path.
What is normally best is to use python virtualenv to have specific python setups for specific tasks. these virtualenvs can be shared amongst users as long as it is always one user (the one who created the virtualenv) who performs the pip installs and as long as the other is only using the virtualenv.
I suggest you try to read about virtualenvs. ( https://virtualenv.pypa.io/en/latest/ )
Very quick introduction
# install virtualenv
python -m pip install --user virtualenv
# create a virtualenv
python -m virtualenv /path/to/virtualenv # e.g /home/pi/virtualenv4service
# activate virtualenv
source /path/to/virtualenv/bin/activate
pip install signalrcore
pip install pyinstaller
pip install any_other_module_you_need
Then call pyinstaller with
pyinstaller service.spec

Packaging pygame python scripts for cross-platform

What is the best way to package python scripts using the pygame module as cross platform executables? I need to be able to compile from my Linux machine. I've tried using PyInstaller, however, when I run the compiled executable, it says it can't find the pygame module. When I running it as a python script through the Wing IDE debug it works fine, so I clearly have pygame installed. However, when I try running without the IDE debug it fails to create a windows. Is there something wrong with my script that it can only be run through Wing IDE? How can I package my game so that it work on any computer, Linux or Windows, without even needed python or pygame? Is this even possible?
My source: https://github.com/william1008/TrappedTux
PyInstaller works fine to build a Windows executable that embed both python and pygame.
Here is the spec file I'm using:
# -*- mode: python -*-
a = Analysis(['MyApplication.py'],
pathex=['.'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
# Remove some weird error message
for d in a.datas:
if 'pyconfig' in d[0]:
a.datas.remove(d)
break
pyz = PYZ(a.pure)
exe = EXE(pyz,
Tree('resource', prefix='resource', excludes=[]),
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='MyApplication.exe',
debug=False,
strip=None,
upx=True,
console=False,
icon="resource/image/MyApplication.ico")
This will bundle everything (resources as well) into a single executable. However, you'll need to use sys._MEIPATH to access those resources. I use this code to detect automatically which path to use:
def resource_path(relative):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return relative
I suggest using cx_Freeze, it’s relatively easy to use and supports both windows and OS X

How To Bundle .jar Files with Pyinstaller

How do you get pyinstaller to bundle .jar files as archives for a python project that utilizes them?
For instance, to make an exe with (I am using pyjnius for handling the sikuli-standalone jar):
# test.py
import os
import sys
# set the classpath so java can find the code I want to work with
sikuli_jar = '/sikuli-api.standalone-1.0.3-Pre-1.jar'
jarpath = os.path.dirname(os.path.realpath(__file__)) + sikuli_jar
os.environ['CLASSPATH'] = jarpath
# now load a java class
from jnius import autoclass
API = autoclass('org.sikuli.api.API')
Pyisntaller creates the (one folder) exe with:
pyinstaller -d test.py
But the jar to the best of my knowledge is not bundled and is inaccessible to the exe unless you manually place it in the folder generated by Pyinstaller
According to the Pyinstaller manual:
"CArchive contains whatever you want to stuff into it. It's very much
like a .zip file."
I then try editing the previously auto-generated test.spec file with:
jar = 'sikuli-api.standalone-1.0.3-Pre-1.jar'
jar_path = 'C:\\Python27\\Lib\\site-packages\\sikuli-0.1-py2.7.egg\\sikuli\\' + jar
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
[('sikulijar', jar_path, 'PKG')],
strip=None,
upx=True,
name='test')
And I try building the exe based on this spec file with:
python C:\workspace\code\PyInstaller-2.1\PyInstaller\build.py --onefile test.spec
But nothing happens and no error returns. Can someone provide a simple step by step tutorial how this could be done? Many thanks!
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
[('sikulijar', jar_path, 'PKG')],
strip=None,
upx=True,
name='test')
change 'sikulijar' in the tuple to just jar (the variable that you have already defined). you need to reference the same name that you have used in code.
However, I'm still trying to get the JVM to initialize properly. I'll post that if I figure that out.
With a valid virtual environment installed, here's how I've packaged a jar interoperating with python via jnius:
addFiles=" \
--add-data project/resources/jnius.so:jnius \
--add-data relative-path-to-jar.jar:resources \ # <-- example jar
--add-data any-other-resources:resources \
"
source ./venv/bin/activate
./venv/bin/pip install -r ./requirements.txt
./venv/bin/pyinstaller --onefile ${addFiles} project/mainModule.py --log-level WARN --hidden-import=jnius_config

pyinstaller: packaging multiple python scripts under Mac

This is my first time using pyinstaller. My goal is to build an .app in Mac Mountain Lion. The app is basically a GUI written in PySide, and I have about 7 different Python scripts + 1 .png file. The main file calls 4 of the files, and the 4 files will call the rest of the 2 files repeatedly. The .png file is nothing but the window logo. Could someone help me with some diagnosis? I do not know what went wrong. I searched a lot of documentations online, i.e., change spec, add import, ... etc. but my app still doesn't run.
FYI, Pyinstaller could generate an app for me, but there are two issues:
Icon is not changed for the app.
App crashes when opened.
My Python version is 2.7.5 and I am using PyInstaller-2.0. Here is my code for packaging:
python pyinstaller.py --onefile --windowed --name=MyApplication -i ~/Documents/AASource/icon.ico ~/Documents/AASource/Scripts/main_file.py
Here is the spec file:
# -*- mode: python -*-
a = Analysis(['/Users/boxuancui/Documents/AASource/Scripts/main_file.py'],
pathex=['/Users/boxuancui/Documents/pyinstaller-2.0'],
hiddenimports=[],
hookspath=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name=os.path.join('dist', 'MyApplication'),
debug=False,
strip=None,
upx=True,
console=False , icon='/Users/boxuancui/Documents/AASource/icon.ico')
app = BUNDLE(exe,
name=os.path.join('dist', 'MyApplication.app'))
Here is part of the crash message:
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000000000054d8
Thanks in advance! Any help will be appreciated!
UPDATE1: I have narrowed down the problem to this: whenever I tried to do the following imports, the created app will crash. Does anyone know why?
import pandas.rpy.common as com
import pandas.io.sql as psql
import rpy2.robjects as robjects
UPDATE2: I take a closer look into pandas.io.sql. There is no such module existing. That's why PyInstaller cannot find it. Where can I find this file? Similarly, rpy2.objects and pandas.rpy.common are missing too.
Can you import pandas.io.sql from your python console?
Are those imports in the main file? If not try doing so

Categories

Resources