Javier Stacul
Javier Stacul

Reputation: 63

Import error using KivyMD and Pyinstaller

When i try to use pyinstaller with 'KivyMD Kitchen Sink' (--one file --debug --clean) i get this:

File "kivymd\theming.py", line 17, in <module>
File "site-packages\kivy\core\text\__init__.py", line 248, in register
# and pass it in context.config token
OSError: File C:\Users\username\AppData\Local\Temp\_MEI92522\kivymd\fonts/Roboto-Regular.ttfs not found
Failed to execute script main

I try to use:

if hasattr(sys, '_MEIPASS'):
    os.chdir(sys._MEIPASS)

in main.py but nothing changed.

Also, i copied Roboto-Regular.ttf to the main.exe's directory without success.

Pyinstaller is working well with other Kivy apps.

UPDATE:

I found a temporaly solution. I used pyinstaller's --onedir command with main.py instead main.spec file:

wine pyinstaller --onedir main.py

Then, i copied kivymd folder (../site-packages/kivyMD) to my app's folder, and the app started without errors.

Anyways, I would like freeze app as a --onefile, Any suggesions? thanks!

Upvotes: 1

Views: 3684

Answers (3)

vishnu sai bhonsle
vishnu sai bhonsle

Reputation: 1

  • use pip install auto-py-to-exe
  • run auto-py-to-exe (in cmd)
  • select additional option in auto-py-exe ui
  • in additional file, add kivymd folder which is copied from site package (auto -py-exe(ui))

Upvotes: 0

ArtemSBulgakov
ArtemSBulgakov

Reputation: 1090

Since KivyMD 0.102.1 there is PyInstaller hook. You can just specify KivyMD's hook directory in your .spec file:

from kivymd import hooks_path as kivymd_hooks_path

a = Analysis(
    # ...
    hookspath=[kivymd_hooks_path],
    # ...
)

You can see Kitchen Sink's pyinstaller.spec file and example in KivyMD's documentation on how to use this hook.


The full pyinstaller.spec file will be:

# -*- mode: python ; coding: utf-8 -*-
import sys
import os
from kivy_deps import sdl2, glew
from kivymd import hooks_path as kivymd_hooks_path
path = os.path.abspath(".")
a = Analysis(
    ["main.py"],
    pathex=[path],
    hookspath=[kivymd_hooks_path],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=None,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=None)
exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
    debug=False,
    strip=False,
    upx=True,
    name="app_name",
    console=False,
)

Upvotes: 2

aleks
aleks

Reputation: 207

There is no hook for kivymd in pyinstaller, it would not recognize package and therefore will be import errors. So what you can do is write a small hook for pyinstaller so it can be recognized during packing.

Perhaps try using my snippet. Tested on Windows 10

from PyInstaller.utils.hooks import (
    collect_data_files, 
    copy_metadata,
    collect_submodules
)

datas = copy_metadata('kivymd')
hiddenimports = collect_submodules('kivymd')

datas = collect_data_files('kivymd')

Reference to PyInstaller hook https://bitbucket.org/snippets/eiNjel/RgdLkG

Create this file in pyinstaller/hooks and you should be fine.

Upvotes: 1

Related Questions