Package for windows (exe) Is it posible to add assets files intro exe file

463 views
Skip to first unread message

Nikola L.

unread,
Apr 7, 2021, 7:17:39 PM4/7/21
to Kivy users support

 I made cross-k little visual tool engine.
 I pack with -single file my exe project file with success .

 I need to provide loading data from json file and images to make my exe file work fine.

 Can i build assets in one big exe file...
 

 

Elliot Garbus

unread,
Apr 7, 2021, 7:46:25 PM4/7/21
to kivy-...@googlegroups.com

Yes, use the datas section of the pyinstaller spec file to specify the location of the data files.

 

https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6ff5ce58-49f3-4eb5-bbb9-b3ff3de786adn%40googlegroups.com.

 

WvdB63

unread,
Apr 8, 2021, 3:49:33 AM4/8/21
to Kivy users support
Have a look at this example: https://stackoverflow.com/questions/39092113/pyinstaller-kivy-clockapp-example-issue/39194161#39194161

Op donderdag 8 april 2021 om 01:46:25 UTC+2 schreef ElliotG:

Elliot Garbus

unread,
Apr 8, 2021, 9:33:39 AM4/8/21
to kivy-...@googlegroups.com

Here is an example spec file that I use.  I create a directory called project_dist and put the spec file there.  This is important to understand the paths.

Reading the kivy log is very useful when trying to debug pyinstaller build problems.

I build a single directory build, and use the Inno Setup Compiler to create a Windows Installer.  It has  a wizard that makes it easy to create an installer.  https://jrsoftware.org/isinfo.php

 

Note the datas section uses a tuple (source file(s), dest dir)

 

# -*- mode: python -*-


import os
from kivy_deps import sdl2, glew

spec_root = os.path.abspath(SPECPATH)
block_cipher =
None
app_name = 'App Name Here'
win_icon = '../Images/app_design_icon.ico'

a = Analysis(['../main.py'],
            
pathex=[spec_root],
            
datas=[('../*.kv', '.'),
                    (
'../Images/*.png', './Images')],
            
hiddenimports=['win32timezone'],           # this is required for filechooser.
            
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=app_name,
         
debug=False,
         
bootloader_ignore_signals=False,
         
strip=False,
         
upx=False,
         
console=False,
         
icon=win_icon)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p)
for p in (sdl2.dep_bins + glew.dep_bins)],
              
strip=False,
              
upx=False,
              
name=app_name)

Nikola L.

unread,
Apr 8, 2021, 12:15:33 PM4/8/21
to Kivy users support

 Thanks All for fast answers , 
  Great support group.



Raja Ramesh

unread,
May 19, 2021, 4:34:50 AM5/19/21
to kivy-...@googlegroups.com
Hi Elliot,

I have an .jpg as background image in the code and i build .exe as single file. In my system it is working fine without any error. But, i am getting error while testing same .exe file on different system.

Validation:
    orientation: 'vertical'
canvas:
Rectangle:
source: 'C:/Users/user/Desktop/images.jpg'
size: self.size
pos: self.pos
ScreenManagment:
        id: sm  


Do we need to add .jpg file path as below in .spec file to match with .jpg path included in the code ? 

a = Analysis(['C:\\Users\\user\\Desktop\\main.py'],
             pathex=['C:\\Users\\user\\AppData\\Roaming\\Python\\Python37\\Scripts'],
             binaries=[],
             datas=[('C:\\Users\\user\\Desktop\\images.jpg','data')]

Thanks,
Raja

Elliot Garbus

unread,
May 19, 2021, 8:43:00 AM5/19/21
to kivy-...@googlegroups.com

Yes, you need to add the jpg file to the datas list.  There is an issue that the datas entry you have proposed will not work.  You will have a different path for the bundled code.

 

I would recommend you put all of the code in a project directory.  Create a subdirectory called images and put all of the images to be used by the program in the images directory.  Use relative paths in you code and you can have the same path for the bundled and unbundled code.

 

Example:

 

ProjectOne

    /ProjectOne

        MySpecFile

    /images

    Main.py

 

Validation:

    orientation: 'vertical'
    canvas:
        Rectangle:
            source: 'images/images.jpg'
            size: self.size
            pos: self.pos

and datas:

datas = [('../Images/*.jpg', './Images')]

Raja Ramesh

unread,
May 20, 2021, 11:29:38 AM5/20/21
to kivy-...@googlegroups.com
Hi Elliot,

I tried as per your suggestion and still I am not able to get the image in .exe file. 

ProjectOne

    /raja

         image_test_with_exe.spec

    /images

    image_test_with_exe.py

      

code:-
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
import os
import sys

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
print(os.path.join(base_path, relative_path))
return os.path.join(base_path, relative_path)
Images = resource_path("images.jpg")

kv='''
<Mainscreen>:
Label:
text: 'Select Task '
color: 0,0,0,1 # to change color
font_size: 20
bold: True
underline: True
size_hint: .3,.2
pos_hint: {'center_x':.5, 'center_y':.9}
FloatLayout:
Button:
text: 'User_Setup'
size_hint: .2,.1
pos_hint: {'center_x':.4, 'center_y':.5}
on_release:
root.manager.transition.direction= 'right'
root.manager.current = "First"

<First>:
Label:
text: 'First Screen '
color: 0,0,0,1 # to change color
font_size: 20
bold: True
underline: True
size_hint: .3,.2
pos_hint: {'center_x':.5, 'center_y':.9}
FloatLayout:
Label:
markup: True
text: 'you clicked [b][color=3333ff][i][size=30]"user_setup button..."[/size][/i][/b][/color]' # changing to italic style
font_size: 20
Button:
text: 'Back'
size_hint: .2,.1
pos_hint: {'center_x':.4, 'center_y':.3}
on_release:
root.manager.transition.direction= 'left'
root.manager.current = "Main"

# Window opens with below code to work with
mailIdValidation:
    orientation: 'vertical'
canvas:
Rectangle:
source: 'images/images.jpg'
size: self.size
pos: self.pos
    ScreenManagment:
id: sm
Mainscreen:
name: 'Main'
First:
name: 'First'
'''

class ScreenManagment(ScreenManager):
pass

class Mainscreen(Screen):
pass

class First(Screen):
pass

class mailIdValidation(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
#images = self.resource_path()

#def resource_path(self, relative_path):
'''def resource_path(self):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
#print(os.path.join(base_path, relative_path))
print(base_path)
return base_path #os.path.join(base_path, relative_path)'''

class MainApp(App):
def build(self):
return Builder.load_string(kv)

MainApp().run()

spec file:-

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

import os
from kivy.deps import sdl2, glew


spec_root = os.path.abspath(SPECPATH)
block_cipher = None


a = Analysis(['../image_test_with_exe.py'],
             pathex=[spec_root],
             binaries=[],
             datas=[('../images/images.jpg', './images')],
             hiddenimports=[],


Thanks,
Raja

Elliot Garbus

unread,
May 20, 2021, 1:11:40 PM5/20/21
to kivy-...@googlegroups.com

Do a single directory build.  See if the structure is what you expect.  Do you see the created images dir.

Raja Ramesh

unread,
May 21, 2021, 5:32:07 AM5/21/21
to kivy-...@googlegroups.com
Hi Elliot,

I am trying to build onefile (.exe) but I am not able to create a single exe file with .jpg file used in the code. can you share an example? 
Also I have attached the code, spec and image which I am trying build one exe file.  

I tried all possibilities to add .jpg files in spec file and I am not able to figure out how the relative path function passes to .spec file. When I search in google they were saying add below method on top of the scripts.
def resourcePath():
    '''Returns path containing content - either locally or in pyinstaller tmp file'''
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS)

    return os.path.join(os.path.abspath("."))

Thanks,
Raja

image_test_with_exe.7z
image_test_with_exe.7z
images.jpg

Elliot Garbus

unread,
May 21, 2021, 8:56:59 AM5/21/21
to kivy-...@googlegroups.com

For all of my kivy apps I do a one directory build and use the Innosoft setup compiler to build a windows installer.  It has a wizard is easy to use, and provides a professional looking windows installer, and the app can be uninstalled from App/Settings in Windows.  https://jrsoftware.org/isinfo.php

 

I did a one exe build of a tkinter based app years ago… and did something similar to your resource code.  I need to go to a different machine to see if I can find that code, If I can I will send it along.

Elliot Garbus

unread,
May 21, 2021, 9:11:02 AM5/21/21
to kivy-...@googlegroups.com

Here is the relevant code from an old one file build.

 
if getattr(sys, 'frozen', False):    # required so iconfile can be packed by pyinstaller
   
application_path = sys._MEIPASS
elif __file__:
    application_path = os.path.dirname(__file__)
iconfile =
'knob.png'
img = PhotoImage(file=os.path.join(application_path, iconfile))

 

The relevant docs: https://pyinstaller.readthedocs.io/en/latest/runtime-information.html#run-time-information

 

Doing a onedir build, I do not need to use the code above.  Using relative paths for data works.

Raja Ramesh

unread,
Jun 8, 2021, 1:05:14 AM6/8/21
to kivy-...@googlegroups.com
Thank you very much for your support Elliot, Finally... I am able to bundle '.png' files into one folder and to single exe . 

Thanks,
Raja 

Reply all
Reply to author
Forward
0 new messages