I am trying to convert my python game - space invaders into an exe. I have seen py2exe and cx_freeze but they seem to compile only 1 singe py file. I also have a bunch images from which I load from 2 resource folders that the modules depend on. Can anyone please help me? Thanks.
Asked
Active
Viewed 3,263 times
2 Answers
2
Easy. Just use the cx_freeze script but modify its scope. Include the pygame libraries, add the tkinter dependency, and include the game file directory.
import cx_Freeze
import sys
import os
# Include TK Libs
os.environ['TCL_LIBRARY'] = r'C:\\Users\\yourusername\\AppData\\Local\\Programs\\Python\\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\\Users\\yourusername\\AppData\\Local\\Programs\\Python\\Python36\tcl\tk8.6'
executables = [
cx_Freeze.Executable(
script="yourClientFile.pyw", # .pyw is optional
base = "Win32GUI"
)
]
cx_Freeze.setup(
name='myName',
options={'build_exe':{'packages':['pygame'], 'include_files':['yourGameDataDirectoryHere']}},
executables = executables,
version = '1.0.0'
)
Note that this will only work with this type of file structure:
Client.py (Runs game off src)
src - |
Game Files here

Mercury Platinum
- 1,549
- 1
- 15
- 28
1
I'm personally familiar with pyinstaller, this is done via the "--onefile" parameter
However for py2exe it has been explained here, https://stackoverflow.com/a/113014/9981387

Reroute
- 265
- 1
- 9
-
sorry could u explain the --onefile parameter, and if possible would u be kind enough to compile the files if I gave them to you via email, my executable is due tomorrow so I would be everso grateful :) – Yasin Khan Jul 02 '18 at 13:33
-
I would prefer to not give out my email, As you have installed py2exe, I will assume you can run "pip install pyinstaller", with this done, you run "pyinstaller --onefile FILE" File being your main python file, it will then complie it down to 1 file, and put it in the "dist" folder of your python installation. – Reroute Jul 02 '18 at 13:42
-
Thanks but I just found out that the --onefile parameter is used to package everything into a single executable file, so that you dont have separate ones for libraries etc. ( according to the pyinstaller documentation). But my issue isn't that I want a single executable file, I just want to compile ALL of my PYTHON FILES into that executable. So I have 4 modules the primary calling module which is main, and then I have the 3 game modes human, alien,coop. – Yasin Khan Jul 02 '18 at 13:43
-
Ok to clarify, the --onefile compiles the main file and all of its imports into a single executable, meaning the .exe will run exactly like when you press run for that file in your IDE, if you want different .exe files for different options, then run pyinstaller on the file you would run to get the result you desire. – Reroute Jul 02 '18 at 13:47
-
ah yep I see thanks ill try it if I get any errors ill let u know – Yasin Khan Jul 02 '18 at 14:11
-
Yep I think I have an issue with when I call other modules I use subprocess.call(module name.py) is there another way of calling ? – Yasin Khan Jul 02 '18 at 15:06
-
It appears you have a few options, the first being --onedir this will create a file for each subprocess child. or https://stackoverflow.com/questions/33020644/python-subprocess-popen-with-pyinstaller using Popen and piping your input and output into the same console – Reroute Jul 02 '18 at 20:49
-
Why are you using `subprocess.call()`? Is it just to run the code in parallel? If so, you could import the modules you require and use the [threading module](https://docs.python.org/3/library/threading.html) instead. – import random Jul 03 '18 at 07:52