packaging for macOs - configparser error

81 views
Skip to first unread message

Joerg B

unread,
Nov 14, 2022, 2:32:39 AM11/14/22
to Kivy users support
Hi there I’m having a question regarding packaging for macOS. I was following the instructions for using pyinstaller. 
The app isn’t starting. It throws an error saying that there is a key error with configparser. 
I guess that means that configparser can’t find the .ini files. I also tried to put all the .py and .ini files in the root directory.
Does anyone have an idea how to sort that out?
Thanks
Joerg

Joerg B

unread,
Nov 14, 2022, 4:17:56 AM11/14/22
to Kivy users support
I was able to narrow it down a bit. If I activate a virtual env and start main.py, it's only working if I cd in the directory where main.py is before. 
Starting it from an other directory like python ~/folder/main.py I'm getting the same error. 
So I'm pretty certain it's not a kivy related question. 

Elliot Garbus

unread,
Nov 14, 2022, 8:11:01 AM11/14/22
to kivy-...@googlegroups.com

Here are a few macos specific fixes.

 

The problem you are having is you need to set the location of the config file. Add the code below to your App class. This will change the location of the config file on MacOS.  You can change this to a different location if you prefer.

 
    def get_application_config(self, defaultpath='%(appdir)s/%(appname)s.ini'):
        if platform == 'macosx':  # mac will not write into app folder
            s = '~/.%(appname)s.ini'
        else:
            s = defaultpath
        return super().get_application_config(defaultpath=s)

 

Here are a few other Mac specific fixes…

The Mac uses higher resolutions icons that Windows.  I create a file called configstartup.py.  This file is imported into main.py before kivy.app.App is imported. I also use this to preserve the Window size between runs.   This repository shows how to maintain Window size: https://github.com/ElliotGarbus/KivyWindowSize

 

 

from configparser import ConfigParser
from kivy.config import Config
from kivy.utils import platform
 
 
"""
configstartup.py is used to set graphics 
 
This code must be at the top of the 'main' executable file.  
Config.set('graphics'...) are used to set the size of the main window.
Win10 requires the windows size to be set prior to App being loaded, to draw smoothly
 
"""
# Default window size and position, also used to set minimum window size
window_width = 1000
window_height = 650
window_top = 50
window_left = 100
 
if platform != 'macosx':
    import os
    ini_file = os.path.join(os.getcwd(), 'your_configfile.ini')
 
    # Use Python lib configparser to read .ini file prior to app startup
    parser = ConfigParser()
    found = parser.read(ini_file)  # created in main.py: build_config()
    if found:
        Config.set('graphics', 'width', parser['Window']['width'])
        Config.set('graphics', 'height', parser['Window']['height'])
        Config.set('graphics', 'position', 'custom')
        Config.set('graphics', 'top', parser['Window']['top'])  # find top and left
        Config.set('graphics', 'left', parser['Window']['left'])
    else:
        Config.set('graphics', 'width', window_width)  # default value match default values in main.py: build_config, on_start
        Config.set('graphics', 'height', window_height)
        Config.set('graphics', 'position', 'custom')
        Config.set('graphics', 'top', window_top)
        Config.set('graphics', 'left', window_left)
if platform == 'macosx':
    Config.set('kivy', 'window_icon','Images/surgeon_app_logo_512.png')
else:
    Config.set('kivy', 'window_icon','Images/surgeon_app_logo_64.png')  # Windows uses a small png
Config.set('kivy', 'exit_on_escape', 0)
Config.set('input', 'mouse', 'mouse,disable_multitouch')
#Config.set('kivy', 'log_level', 'error')

 

 

For reference here is my mac specfile:

# -*- mode: python -*-
 
block_cipher = None
app_name = 'MyApp Name Here'
mac_icon = '../Images/logo_512.icns'
 
 
a = Analysis(['../main.py'],
             pathex=[],
             binaries=[],
             datas=[('../*.kv', '.'),
                    ('../Images/*.png', './Images')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=['_tkinter', 'Tkinter', 'enchant', 'twisted'],
             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)
 
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               name=app_name)
 
app = BUNDLE(coll,
             name=app_name + '.app',
             icon=mac_icon,
             bundle_identifier=None)

--
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/dd363559-5a62-4cc1-92b3-b25cef17399cn%40googlegroups.com.

 

Joerg B

unread,
Nov 14, 2022, 11:55:34 AM11/14/22
to Kivy users support
Elliot, thanks a lot! 
Reply all
Reply to author
Forward
0 new messages