MagTun
MagTun

Reputation: 6185

How to package a Kivy app with Pyinstaller

I have a lot of troubles following the instructions form the Kivy website, many steps aren't explained like what should I answer to the warning.

WARNING: The output directory "..." and ALL ITS CONTENTS will be REMOVED! Continue? (y/n) 

Even if I choose y, the folder isn't removed.

Also should I always add these lines:

in the .spec file? Why are they necessary?

Not many info is available for Kivy.

Upvotes: 1

Views: 3339

Answers (3)

Ivan
Ivan

Reputation: 769

It also works if in the 'a = Analysis...' block in the spec file one substitutes

hiddenimports=[]

for

hiddenimports=['win32file', 'win32timezone']

for win32file, win32timezone or for whatever files are missing

Upvotes: 1

MagTun
MagTun

Reputation: 6185

Because I spent a lot of time understanding how I should package my app, here are some instructions that would have really helped me.

Some info are available at http://pythonhosted.org/PyInstaller/

Python 3.6 as of march 2017 Because packaging my app gave me the error IndexError: tuple index out of range, I had to install the developement version of PyInstaller:

pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

Step 1:

I moved all the files of MyApp in a folder "C:\Users\<username>\Desktop\MyApp": the .py, the .kv and the images and I created an icon.ico. I created another folder C:\Users\<username>\Desktop\MyPackagedApp. In this folder I press Shift+right click and select open command window here.

Then I pasted this:

python -m PyInstaller --name MyApp --icon "C:\Users\<username>\Desktop\MyApp\icon.ico" "C:\Users\<username>\Desktop\MyApp\myapp.py"

This creates two folders, build and dist, and a .spec file. In dist/MyApp, I can find a .exe. Apparently, if my app is really simple (just one label), the packaged app can works without the Step 2.

Step 2:

The second step involves editing the .spec file. Here is an exemple of mine. (cf Step 3, for the explanations about my_hidden_modules)

I go back to the cmd, and enter

python -m MyApp myapp.spec

I then got this warning:

WARNING: The output directory "..." and ALL ITS CONTENTS will be REMOVED! Continue? (y/n) 

I enter y and then press enter.

Because I choosed y, I was surpised that the folder build was still there and that the dist/MyApp was still containing many files. But this is normal. PyInstaller can output a single file .exe or a single folder which contains all the script’s dependencies and an executable file. But the default output is a single folder with multiple files.

Step 3: adding hidden modules

When I click on the myapp.exe in dist/MyApp, the app crashed. In the log C:\Users\.kivy\logs\ I could find 2 errors: ModuleNotFoundError: No module named 'win32timezone' and SystemError: <class '_frozen_importlib._ModuleLockManager'>. Because of this I had to edit the .spec file and add these lines:

my_hidden_modules = [
         ( 'C:\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\win32\\lib\\win32timezone.py', '.' )
         ]

in a = Analysis I changed datas = [] to datas = my_hidden_modules,

Apparently this is because I used a FileChooser widget.

Upvotes: 5

Peter Badida
Peter Badida

Reputation: 12159

So, the line:

ALL ITS CONTENTS will be REMOVED!

yes, it will be removed AND replaced later with new files. Check the date. I think it prints permission denied if it can't do such a thin both for files and the whole folder, so you'd notice it. It's important though, because you need to add additional files into your folder.

Those additional files of two types:

  • kivy dependencies
  • application data

Dependencies are just binaries (+/- loaders, licenses, or so), you get them through the *[Tree(p) ...] piece of code, which is just a command for "get all files from that folder". Without them Kivy won't even start.

Similarly to that, the second Tree(<app folder>) does the same, but for your own files such as .py files, .kv files, images, music, databases, basically whatever you create.

Obviously if you remove the deps, app won't start and if you remove app data, you'll get some path errors and most likely crash. You don't want any of that :P

Upvotes: 1

Related Questions