Repository: kivy/kivy

Issue:
Can't create package for windows with kivy 1.9 portable
I'm looking to port an existing kivy 1.8 project to kivy 1.9. I've just downloaded the portable version and have the application working.

However when packaging the app using pyinstaller and the instructions on http://kivy.org/docs/guide/packaging-windows.html the app packages, but on execution immediately fails with error:

```
Traceback (most recent call last):
  File "<string>", line 34, in <module>
ImportError: No module named pygame.pkgdata
```

I've tried using my old .spec file and generating a new one with exactly the same results.

I'm a bit mystified where this is coming from as pygame isn't imported anywhere in my application and I thought it had been replaced with sdl2 in kivy 1.9. I'm also confused that the application works when run directly.

Anyone come across this issue or can point me in the right direction?



Discussion:
All packaging and installation docs are outdated in some degree, see https://github.com/kivy/kivy/issues/3260.

@jamiecollinson https://github.com/kivy/kivy/blob/master/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py#L32 could you comment out all the files from here and see if that helps?

@akshayaurora commenting out lines 32-42 gets me past the initial error but still fails on:

```
[CRITICAL          ] [Window      ] Unable to find any valuable Window provider at all!
sdl2 - Exception: SDL2: Unable to load image
```

Also looks like it's not loading opencv (which I'm using for camera) because numpy is failing to import, but I don't think that's directly connected as the app can load without a camera provider.

Ok, we need to provide sdl2 hooks, out at least say which dlls need to be included in the app. In particular, you need to include the DLLs from SDL2/bin and glew32.dll from MinGW/bin.

Yes, that is correct. Actually, what happened is that Kivy didn't find the SDL2 library for png loading. Remember that SDL2 background is png image. The solution is easy, just copy from SDL2\bin\ folder in the kivy portable dist the file libpng16-16.dll to your app package folder. 

Thanks @h-sol & @matham - manually copying `libpng16-16.dll` gets me past the SDL2 error and the application mostly runs okay now.

I'm still running into the problem that numpy / opencv don't seem to be being imported and so the application fails on camera start. I'm confused by this as other imported libraries (e.g. peewee) are importing fine. Any pointers?

@jamiecollinson you may need to add them to hidden imports in the spec file.

@h-sol and @jamiecollinson do you have some example code that replicates the libpng16-16.dll issue? Because I cannot seem to replicate it.


Resolving patch:
diff --git a/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py b/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py
--- a/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py
+++ b/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py
@@ -29,15 +29,17 @@
 environ['KIVY_MODULES_DIR'] = join(root, 'modules')
 environ['KIVY_EMBED'] = '1'
 
+
 # Monkey-patch pygame to get around an issue with Pygame window icon and
 # PyInstaller 2.1. See kivy issue #1638
-# Uncomment the following to package pygame
-#import pygame.pkgdata
-#_original_getResource = pygame.pkgdata.getResource
-#
-#
-#def getResource(identifier, *args, **kwargs):
-#    if identifier == 'pygame_icon.tiff':
-#        raise IOError()
-#    return _original_getResource(identifier, *args, **kwargs)
-#pygame.pkgdata.getResource = getResource
+def getResource(identifier, *args, **kwargs):
+    if identifier == 'pygame_icon.tiff':
+        raise IOError()
+    return _original_getResource(identifier, *args, **kwargs)
+
+try:
+    import pygame.pkgdata
+    _original_getResource = pygame.pkgdata.getResource
+    pygame.pkgdata.getResource = getResource
+except ImportError:
+    pass


Test patch: