Python – Why is the .exe file created by pyinstaller not working

exepygamepyinstallerpython

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

Best Answer

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!
Related Topic