We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py`
Content:
```
1 from os.path import join, dirname
2 from os import environ, chdir, putenv
3 import sys
4 
5 root = 'kivy_install'
6 if hasattr(sys, '_MEIPASS'):
7     # PyInstaller >= 1.6
8     chdir(sys._MEIPASS)
9     root = join(sys._MEIPASS, root)
10 elif '_MEIPASS2' in environ:
11     # PyInstaller < 1.6 (tested on 1.5 only)
12     chdir(environ['_MEIPASS2'])
13     root = join(environ['_MEIPASS2'], root)
14 else:
15     chdir(dirname(sys.argv[0]))
16     root = join(dirname(sys.argv[0]), root)
17 
18 
19 sys.path += [join(root, '_libs')]
20 
21 if sys.platform == 'darwin':
22     sitepackages = join(root, '..', 'sitepackages')
23     sys.path += [sitepackages, join(sitepackages, 'gst-0.10')]
24     putenv('GST_REGISTRY_FORK', 'no')
25 
26 environ['GST_PLUGIN_PATH'] = join(root, '..', 'gst-plugins')
27 environ['KIVY_DATA_DIR'] = join(root, 'data')
28 environ['KIVY_EXTS_DIR'] = join(root, 'extensions')
29 environ['KIVY_MODULES_DIR'] = join(root, 'modules')
30 environ['KIVY_EMBED'] = '1'
31 
32 # Monkey-patch pygame to get around an issue with Pygame window icon and
33 # PyInstaller 2.1. See kivy issue #1638
34 # Uncomment the following to package pygame
35 #import pygame.pkgdata
36 #_original_getResource = pygame.pkgdata.getResource
37 #
38 #
39 #def getResource(identifier, *args, **kwargs):
40 #    if identifier == 'pygame_icon.tiff':
41 #        raise IOError()
42 #    return _original_getResource(identifier, *args, **kwargs)
43 #pygame.pkgdata.getResource = getResource
44
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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

{"golden_diff": "diff --git a/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py b/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py\n--- a/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py\n+++ b/kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py\n@@ -29,15 +29,17 @@\n environ['KIVY_MODULES_DIR'] = join(root, 'modules')\n environ['KIVY_EMBED'] = '1'\n \n+\n # Monkey-patch pygame to get around an issue with Pygame window icon and\n # PyInstaller 2.1. See kivy issue #1638\n-# Uncomment the following to package pygame\n-#import pygame.pkgdata\n-#_original_getResource = pygame.pkgdata.getResource\n-#\n-#\n-#def getResource(identifier, *args, **kwargs):\n-#    if identifier == 'pygame_icon.tiff':\n-#        raise IOError()\n-#    return _original_getResource(identifier, *args, **kwargs)\n-#pygame.pkgdata.getResource = getResource\n+def getResource(identifier, *args, **kwargs):\n+    if identifier == 'pygame_icon.tiff':\n+        raise IOError()\n+    return _original_getResource(identifier, *args, **kwargs)\n+\n+try:\n+    import pygame.pkgdata\n+    _original_getResource = pygame.pkgdata.getResource\n+    pygame.pkgdata.getResource = getResource\n+except ImportError:\n+    pass\n", "issue": "Can't create package for windows with kivy 1.9 portable\nI'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.\n\nHowever 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:\n\n```\nTraceback (most recent call last):\n  File \"<string>\", line 34, in <module>\nImportError: No module named pygame.pkgdata\n```\n\nI've tried using my old .spec file and generating a new one with exactly the same results.\n\nI'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.\n\nAnyone come across this issue or can point me in the right direction?\n\n", "before_files": [{"content": "from os.path import join, dirname\nfrom os import environ, chdir, putenv\nimport sys\n\nroot = 'kivy_install'\nif hasattr(sys, '_MEIPASS'):\n    # PyInstaller >= 1.6\n    chdir(sys._MEIPASS)\n    root = join(sys._MEIPASS, root)\nelif '_MEIPASS2' in environ:\n    # PyInstaller < 1.6 (tested on 1.5 only)\n    chdir(environ['_MEIPASS2'])\n    root = join(environ['_MEIPASS2'], root)\nelse:\n    chdir(dirname(sys.argv[0]))\n    root = join(dirname(sys.argv[0]), root)\n\n\nsys.path += [join(root, '_libs')]\n\nif sys.platform == 'darwin':\n    sitepackages = join(root, '..', 'sitepackages')\n    sys.path += [sitepackages, join(sitepackages, 'gst-0.10')]\n    putenv('GST_REGISTRY_FORK', 'no')\n\nenviron['GST_PLUGIN_PATH'] = join(root, '..', 'gst-plugins')\nenviron['KIVY_DATA_DIR'] = join(root, 'data')\nenviron['KIVY_EXTS_DIR'] = join(root, 'extensions')\nenviron['KIVY_MODULES_DIR'] = join(root, 'modules')\nenviron['KIVY_EMBED'] = '1'\n\n# Monkey-patch pygame to get around an issue with Pygame window icon and\n# PyInstaller 2.1. See kivy issue #1638\n# Uncomment the following to package pygame\n#import pygame.pkgdata\n#_original_getResource = pygame.pkgdata.getResource\n#\n#\n#def getResource(identifier, *args, **kwargs):\n#    if identifier == 'pygame_icon.tiff':\n#        raise IOError()\n#    return _original_getResource(identifier, *args, **kwargs)\n#pygame.pkgdata.getResource = getResource\n", "path": "kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py"}], "after_files": [{"content": "from os.path import join, dirname\nfrom os import environ, chdir, putenv\nimport sys\n\nroot = 'kivy_install'\nif hasattr(sys, '_MEIPASS'):\n    # PyInstaller >= 1.6\n    chdir(sys._MEIPASS)\n    root = join(sys._MEIPASS, root)\nelif '_MEIPASS2' in environ:\n    # PyInstaller < 1.6 (tested on 1.5 only)\n    chdir(environ['_MEIPASS2'])\n    root = join(environ['_MEIPASS2'], root)\nelse:\n    chdir(dirname(sys.argv[0]))\n    root = join(dirname(sys.argv[0]), root)\n\n\nsys.path += [join(root, '_libs')]\n\nif sys.platform == 'darwin':\n    sitepackages = join(root, '..', 'sitepackages')\n    sys.path += [sitepackages, join(sitepackages, 'gst-0.10')]\n    putenv('GST_REGISTRY_FORK', 'no')\n\nenviron['GST_PLUGIN_PATH'] = join(root, '..', 'gst-plugins')\nenviron['KIVY_DATA_DIR'] = join(root, 'data')\nenviron['KIVY_EXTS_DIR'] = join(root, 'extensions')\nenviron['KIVY_MODULES_DIR'] = join(root, 'modules')\nenviron['KIVY_EMBED'] = '1'\n\n\n# Monkey-patch pygame to get around an issue with Pygame window icon and\n# PyInstaller 2.1. See kivy issue #1638\ndef getResource(identifier, *args, **kwargs):\n    if identifier == 'pygame_icon.tiff':\n        raise IOError()\n    return _original_getResource(identifier, *args, **kwargs)\n\ntry:\n    import pygame.pkgdata\n    _original_getResource = pygame.pkgdata.getResource\n    pygame.pkgdata.getResource = getResource\nexcept ImportError:\n    pass\n", "path": "kivy/tools/packaging/pyinstaller_hooks/rt-hook-kivy.py"}]}