3

I built a simple "flappy bird" game with pygame and then tried to convert the .py script with pyinstaller into an .exe with pyinstaller flappybird.py. But when I try to execute the .exe, the command prompt and the game window open for about 2 seconds without displaying any errors and without showing anything from the game, which works fine when executed as python script via flappybird.pyin the command prompt. I am using the newest version of pyinstaller and I don´t have any idea why this isn´t working, since it worked like a charm with other games I wrote before.

Thanks for your help ;D

Luap555
  • 51
  • 1
  • 1
  • 4
  • The problem may be due to loading resources whose locations are different with respect to the executable compared to your script. Please edit your question to include a [mcve] so it's easier to help you. Perhaps this [question and answers](https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile) will help you. – import random Apr 03 '19 at 23:25

1 Answers1

1

Try opening a command prompt and navigating to the folder you installed it. Then run it via flappybird.exe (or whatever you titled it) and check that the error doesn't show up within your terminal. It sounds like it hits an error that crashes it, but immediately closes before you can read the error. Therefore running it from terminal allows it to have a window that doesn't disappear and it can print an error message there if there is one (this may only tell you errors during startup though, I'm not sure). I typically prefer to use a GUI that has a section of text that updates and I can use this to essentially "print" statuses of my compiled programs into this box, perhaps you could use a similar technique? Best of luck!

update

With your code I was able to compile it and run it with no problems (using cx-freeze). My setup.py file was:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
additional_modules = []

build_exe_options = {"includes": additional_modules,
                     "packages": ["pygame", "random", "sys", "pyglet"],
                     "excludes": ['tkinter'],
                     "include_files": ['icon.ico', 'res']}

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="Flappy Bird",
      version="1.0",
      description="Flap around",
      options={"build_exe": build_exe_options},
      executables=[Executable(script="flappybird.py", base=base)])

You can get your executable working if you

  • pip install cx_freeze

  • copy/paste the code above into a file setup.py (placed right next to your script)

  • open folder in command prompt and run python setup.py build

You may need to run the command again if it fails (something to do with trying to read from a folder it hasn't made yet).

  • Now you have a new folder build and your executable is inside it!
Community
  • 1
  • 1
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • Thank you for your reply, even if i start the exe via the console, no error shows up. Can you explain more accurate how you use an GUI? I don´t quite get what you mean. – Luap555 Apr 03 '19 at 20:54
  • Would it be possible for you to link the source code or executable? – Reedinationer Apr 03 '19 at 20:55
  • Can you try to convert it via pyinstaller? I will try it with cx_freeze later, i had problems installing it because visual c++ 14.0 was missing – Luap555 Apr 04 '19 at 09:04
  • I'll leave it for somebody else to post a `pyinstaller` solution. I prefer `cx_freeze` because the developer (you) specifies what packages to include/exclude as well as any additional files/folders your program needs. `pyinstaller` on the other hand gets all the files itself (an incomplete list of supported packages: https://github.com/pyinstaller/pyinstaller/wiki/Supported-Packages), but can often encounter errors like discussed at https://pyinstaller.readthedocs.io/en/stable/operating-mode.html#analysis-finding-the-files-your-program-needs – Reedinationer Apr 04 '19 at 17:08
  • Thank you anyways it worked after finally getting cx_freeze to work! – Luap555 Apr 04 '19 at 19:36
  • @Luap555 Awesome! It's a pretty sweet game :) just needs maybe some adjusting with the flaps or gravity or something, seems a lot harder than the actual game was XD – Reedinationer Apr 04 '19 at 19:37
  • Oh yes that's true – Luap555 Apr 04 '19 at 20:26