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.
Related
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)
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
I have a project that runs from GUI.py and imports modules I created. Specifically it imports modules from a "Library" package that exists in the same directory as GUI.py. I want to freeze the scripts with cx_Freeze to create a windows executable, and I can create the exe, but when I try to run it, I get: "ImportError: No module named Library."
I see in the output that all the modules that I import from Library aren't imported. Here's what my setup.py looks like:
import sys, os
from cx_Freeze import setup, Executable
build_exe_options = {"packages":['Libary', 'Graphs', 'os'],
"includes":["tkinter", "csv", "subprocess", "datetime", "shutil", "random", "Library", "Graphs"],
"include_files": ['GUI','HTML','Users','Tests','E.icns', 'Graphs'],
}
base = None
exe = None
if sys.platform == "win32":
exe = Executable(
script="GUI.py",
initScript = None,
base = "Win32GUI",
targetDir = r"built",
targetName = "GUI.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
base = "Win32GUI"
setup( name = "MrProj",
version = "2.0",
description = "My project",
options = {"build.exe": build_exe_options},
#executables = [Executable("GUI.py", base=base)]
executables = [exe]
)
I've tried everything that I could find in StackOverflow and made a lot of fixes based upon similar problems people were having. However, no matter what I do, I can't seem to get cx_freeze to import my modules in Library.
My setup.py is in the same directory as GUI.py and the Library directory.
I'm running it on a Windows 7 Laptop with cx_Freeze-4.3.3.
I have python 3.4 installed.
Any help would be a godsend, thank you very much!
If Library (funny name by the way) in packages doesn't work you could try as a workaround to put it in the includes list. For this to work you maybe have to explicitly include every single submodule like:
includes = ['Library', 'Library.submodule1', 'Library.sub2', ...]
For the include_files you have to add each file with full (relative) path. Directories don't work.
You could of course make use of os.listdir() or the glob module to append paths to your include_files like this:
from glob import glob
...
include_files = ['GUI.py','HTML','Users','E.icns', 'Graphs'],
include_files += glob('Tests/*/*')
...
In some cases something like sys.path.insert(0, '.') or even sys.path.insert(0, '..') can help.
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.
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]))