I am trying to just convert my pygame python project to a .exe file using cx_Freeze. The setup file executes correctly and without error, but the issue is that when I run my .exe file the console window will open and close and my game window will not appear.
The setup.py I am using:
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\\MY_USERNAME\\AppData\\Local\\Programs\\Python\\Python35-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\\MY_USERNAME\\AppData\\Local\\Programs\\Python\\Python35-32\\tcl\\tk8.6"
import cx_Freeze
executables = [cx_Freeze.Executable("main.py")]
cx_Freeze.setup(
name="Shooty",
options={"build_exe": {"packages": ["pygame"], "include_files": ["character.png"]}},
description="Shooty, An epic platformer!",
executables=executables
)
I had a TCL error so I added the first three lines. Then after that everything should be correct. I have already seen similar posts but none seem to help.
cx_freeze app opens then closes quickly < This user just has a simple typographical error
python program works but .exe does not open cx_Freeze < Has no anwser, and no helpful comment.
Executable generated with cx_freeze opens for an instant before closing < This user actually gets an error. I don't.
To clarify/TLDR: I am using cx_Freeze to convert a pygame game to .exe format. I only have one image called character.png. The build executes perfectly without errors (as long as I include the first three lines, which I have) The only issue is: upon running the exe, the console opens but not my game window and the console closes immediately.
I copied your setup script and tested it with one of my own Pygame projects. It didn't work for me either at first, so I investigated the problem and found a bug on Bitbucket: https://bitbucket.org/anthony_tuininga/cx_freeze/issues/211/if-name-main-doesnt-work-in
Apparently there is a bug, specifically with cx-Freeze version 5.0, that the line if __name__ == "__main__":
will not work. But when I replaced it with if __name__.endswith('__main__'):
as suggested in the bug report, then my Pygame app worked with cx-Freeze, so this fixed the problem for me.
In addition, I would suggest adding the keyword argument base="Win32Gui"
to cx_Freeze.Executable
, so that the console window doesn't show up behind the Pygame window, unless if you want it. Also, Tkinter is completely unnecessary for a Pygame app, so you can add "excludes": ["Tkinter"]
(or lowercase tkinter
in Python 3) to the options dict and get rid of the first three lines of your script.