How to convert multiple python files to EXE - python

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.
https://i.stack.imgur.com/y6h7M.png

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

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

Related

Error when trying to turn a python script using ursina into an executable

So I was using cx_Freeze in order to turn my python script using ursina into an executable but then this happened : Error
What am I supposed to do?
This is what my folder content and setup.py file looked like when I had the error : Content
Try to add 'ursina' to the packages list of the build_exe_options in the setup.py script.
EDIT: try also to add src to the include_files list of the build_exe_options:
build_exe_options = {'packages': ['ursina'], 'include_files': ['src']}
# ...
setup( name = ..., # complete!
...
options = {'build_exe': build_exe_options},
executables = [Executable(...)])
See the cx_Freeze documentation for further details.
From the original Ursina engine's folder copy the application.py file and in the build folder, you will find exe.win-amd64-3.9 folder , inside that there will be a folder named lib, inside that there will be ursina folder you have to paste it here (if you are using default download location then this is the application.py's path: C:\Users\welcome\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\application.py)

Compiling python project with Cx-Freeze

I'm trying to make and executable from a project I'm working on, to test it on a different computer, without requiring python installed.
I want to use cx freeze to build and exe file, and this is my project tree:
- Project
- objects
blocks.py
enemy.py
item.py
player.py
__init__.py
- scripts
camera.py
chunk_manager.py
__init__.py
- textures
- items
- player
- terrain
- UI
- void
index.py
__init__.py
main.py
settings.py
setup.py
map.json
As you can probably tell, this is a game.
Anyways, what I have done is executing just cxfreeze main.py --target-dir=./ dist and it generated a build directory with a lot of stuff in it.
It generated a linux executable, but thats fine, I want to test if I can make it python-independent first, and I'll solve the .exe part later.
So, I executed the file, and nothing happened. Then I tried running from the terminal and it said that it was missing the camera.py file. I went into the generated directory and I found out that cxfreeze had not copied that file. I tried moving in in there myself, but it did not work.
I started checking for other files, and I found out that only the textures had been copied.
I thought this was just because of the one line command I used, so I am now trying to make a setup.py file (as you can see in the file tree) but I am lost in how to import my custom packages (objects, scripts, and textures) and the files in the same directory as main.py
This is how my setup.py file looks like:
import sys
from cx_Freeze import setup, Executable
options = {
'build_exe': {
'includes': ['camera', 'chunk_manager'], 'path': sys.path + ['scripts']
}
}
setup(
name = "Void Boats",
version = "alpha0.1",
options = options,
executables = [Executable("main.py")])
I am not sure how would I put all the packages in the 'include' section, and I can't find anything on the internet that uses more than a simple file like helloworld.py, and the samples of cxfreeze on their github only show how to import files from one directory. Do I have to move everything into one single package and put all the files in the same 'include'? Or can I have one include for each package I have? Any help would be pretty much appreciated.
Edit: I'm running on ParrotSec 4.9 (Debian based)
Use "python -m setup.py build" on cmd, that will build exe.
Example setup with extra folders:
from cx_Freeze import setup, Executable
import sys
version = "0.0.6"
build_options = {
"packages": [],
"excludes": [],
"build_exe": "X:\\builds\\",
"include_files": ["Sequence_Sample/", "icons/"],
}
base = "Win32GUI" if sys.platform == "win32" else None
executables = [Executable("MainUIController.py", base=base, targetName="pym")]
setup(
name="Python Image Macro Project",
version=version,
description="Image Based macro project.",
options={"build_exe": build_options},
executables=executables,
)
Python 3.8 and later has problem with cx_freeze 6.1 - not copying python dll.
cx_freeze 6.2 is strongly recommended if that's the case.
You'll have to clone cx_freeze and build it, then install it to use 6.2.

cx_Freeze does not include .py files from subdirectories

I'm building a program with Python 3.3, Pyside and lxml amongst others. cx_Freeze was working like a charm until I starting sorting my modules into subdirectories for neatness. cx_Freeze works but reports the modules as missing and the output .exe file fails to load, but there are no issues opening the program with the .py file.
My folder structure is simple and only has 2 sub-folders called parsers and constants.
The cx_Freeze documentation allows for adding a path variable, but this doesn't search for the modules. All help appreciated.
import sys
from cx_Freeze import setup,Executable
includefiles = ['open.png', 'appicon.png']
includes = []
excludes = ['Tkinter']
packages = ['lxml._elementpath','lxml.etree']
build_exe_options = {'excludes':excludes,'packages':packages,'include_files':includefiles}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = 'Config-Sheets',
version = '0.1',
description = 'Convert system configuration into .XLSX files',
author = 'ZZZ',
author_email = 'XXX#YYY.com',
options = {'build_exe': build_exe_options },
executables = [Executable('main.py', base=base)]
)
Results in and an exe that can't run.
Missing modules:
...
? parsers.parsercelerra imported from main__main__
? parsers.parserVPLEX imported from main__main__
? parsers.parserrain imported from main__main__
You should be able to include your packages very simply:
packages = ['lxml._elementpath','lxml.etree', 'parsers', 'constants']
Of course the directories must contain __init__.py in order to be considered packages.
If this doesn't achieve the desired effect then it would be useful to see the import mechanism used in main.py to pull these files in. If it's doing anything other than a straightforward import (or from x import) this can give cx_Freeze problems finding it.
You should include your .py files in the setup.py script. If the file is not a part of Python itself, cx_freeze needs to know that you want those files included. You should add your extra .py files with their paths to the includefiles list. For instance:
import sys
from cx_Freeze import setup,Executable
includefiles = ['open.png', 'appicon.png', 'parsers\xtra_file1.py','constants\xtra_file2.py']
includes = []
excludes = ['Tkinter']
packages = ['lxml._elementpath','lxml.etree']
build_exe_options = {'excludes':excludes,'packages':packages,'include_files':includefiles}
...
Hope this helped.

Cx_Freeze - Include Modules automatically

should I include modules that I have used in my .py like os module in code below or its done automatically?and what about exclude?I have used pyqt4 in my .py is it necessary to add its name in this setup.py file?
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "my-app",
version = "0.9.0",
description = "Copyright 2013",
options = {"build_exe": build_exe_options},
executables = [Executable("my_module.py", base=base)])
As the comment says, dependencies are automatically detected, but you sometimes need to fine tune them manually. os and tkinter are just there as examples, you probably don't need them for your project. Generally, anything you've imported can be detected, but if you load plugin libraries some other way, it won't find them, so you need to specify them.
Try freezing it, and see if it fails because anything is missing, then go back and add that to packages.

Making a standalone .exe file of a python script

I have a python script which contains:
tempBook = temp.Workbooks.Open("c:\users\CNAME\desktop\Template.xlsx")
Everything works fine but when I create a .exe of my script the Template.xlsx is not included in its 'build' folder, it needs Template.xlsx to be present on the desktop. I want to make a portable exe.
Is there any way so that it can be included in the build, and make a standalone exe without any dependencies?
You need to move the file to your package, and list it in your setup.py:
setup(
# ...
data_files = [('', ['Tempate.xlsx',])],
)
See the data_files documentation for py2exe, which includes a utility function for automating adding data files.
The files will be added in your app root. From your main script, you can determine your app root by using:
import os
try:
approot = os.path.dirname(os.path.abspath(__file__))
except NameError: # We are the main py2exe script, not a module
import sys
approot = os.path.dirname(os.path.abspath(sys.argv[0]))

Categories