I want to compile a python script using the cx_Freeze module. For this I write the following setup.py file:
from cx_Freeze import setup, Executable
executables = Executable(script = "Example5.py",icon = "icon.ico")
zip_include_packages = ["collections", "importlib", "encodings"]
excludes = [
'unicodedata', 'logging', 'unittest', 'email', 'html', 'http', 'urllib',
'bz2'
]
options = {
"build_exe": {
"include_msvcr": True,
"excludes": excludes,
"zip_include_packages":zip_include_packages,
"build_exe": "Test compiling",
}
}
setup(
name='Test',
version='1.0.0',
description='Testing compile',
executables=[executables],
options=options
)
This script doesn't want to compile, cx_Freeze is failing with error:
Fatal Python error: Py_Initalize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000bf4 (most recent call first)
But if I replace this line:
executables = Executable(script = "Example5.py",icon = "icon.ico")
by:
executables = Executable(script = "Example5.py")
the script is working and creates a .exe file.
I don't understand why the icon is not set.
Source code: https://github.com/Bus-Artyom/Test_compile
Thank you in advance.
It seems that your icon.ico is not a valid .ico file (but rather a .png file which has been renamed to .ico?).
Try with a valid .ico file.
Related
I've built an app using Tkinter, and when compiling it with cx_Freeze I get no issues.
When I run the .exe generated on my machine, and my virtual test machine, I have no issues. However, someone currently testing my app is encountering this issue:
Previously they had been encountering this issue:
I fixed the issue of 'brotlicffi' not being found by bypassing py7zr by throwing it into a try statement, as it's not completely essential for the app to function. However, cefpython3 is.
What I find very confusing is that the paths in the traceback errors are all my paths, I've encountered this before with cx_Freeze, which threw me off at first but dismissed it as a possible quirk of cx_Freeze/Python.
Here is my cx_Freeze setup.py:
dir_path = os.getcwd()
interface_path = os.path.join(dir_path, "interface")
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
options = {"build_exe": {
"packages": [
"base64",
"re",
"os",
"inspect",
"_winapi",
"json",
"time",
"selenium",
"webdriver_manager",
"tqdm",
"pdfrw",
"cefpython3",
"customtkinter",
"tkinter",
"pyglet",
"urllib",
"py7zr",
"brotli",
"brotlicffi", # Needed for py7zr
"winreg",
"requests",
"shutil",
"stat",
"inspect"
],
# "path": [
# "C:\\Users\\aaron\\PycharmProjects\\slowly scraper\\venv\\Lib\\site-packages"
# ],
"include_files": [
# "yellow.json",
("venv\\Lib\\site-packages\\brotli.py", "lib\\brotli.py"),
"interface"
# "cefpython3"
]
# "replace_paths": [("*", "")]
}
}
executables = [cx_Freeze.Executable(
"main.py",
base=base,
target_name="SLD.exe",
icon=os.path.join(interface_path, "SLD_icon.ico")
)
]
cx_Freeze.setup(
name="Slowly Letter Downloader",
options=options,
author="PastaSource",
version="0.1",
description="Automates the downloading of letters from Slowly",
executables=executables
)
If it helps, I'm running this all in Pycharm, the includes the compilation of the cx_Freeze application.
Hello I'm a newbie in python.
I am trying to make an msi file.
It works fine when I run the exe file in the built directory.
But the shortcut that I made on the DesktopFolder won't work.
The reason seems like the shortcut file can't reference UI files.
When I copy & paste the UI files on the DesktopFolder, it does work.
Is there a way to execute shortcut file properly without moving UI files?
Here's my source code.
import sys
from cx_Freeze import setup, Executable
company_name = '####'
product_name = 'Robot Parser V1.0'
exebuildOptions = dict(packages=[],
excludes=[],
includes=['atexit'],
include_files=['qwed.ui', 'sub.ui'])
bdist_msi_options = { 'upgrade_code': '{parser-04291986}',
'add_to_path': False,
'initial_target_dir': r'[ProgramFilesFolder]\%s\%s' % (company_name, product_name)}
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [Executable('__init__.py', base=base, shortcutName="Robot.ini Parser V1.0",
shortcutDir="DesktopFolder")]
setup(name='Roboparser',
version='0.1',
description='Sample cx_Freeze PyQt4 script',
options={'build_exe' : exebuildOptions, 'bdist_msi': bdist_msi_options},
executables=executables
)
Thank you for your help in advance.
I have a pretty hefty python script that I'm trying to cx_freeze, however when I run the executable file I keep getting the same error and it appears to be related to the docx module.
I'm using Python 3.3.5 with docx 0.7.6-py33 on a Windows 8.1 machine.
This is my setup script.
from cx_Freeze import setup, Executable
includefiles = ['logo.ico','db.db','dbloc.bin']
includes = []
excludes = []
packages = ['tkinter','docx','sys', 'sqlite3', 'os', 'hashlib', 'random', 'uuid', 'base64', 'tempfile', 'win32api',
'winreg', 'ntplib', 'winsound', 'time', 'csv', 'webbrowser', 'inspect','datetime', 'decimal', 'ctypes',
'win32com.client','operator']
exe = Executable(
# what to build
script = "NEPOS.py",
initScript = None,
base = 'Win32GUI',
targetName = "Nepos.exe",
copyDependentFiles = True,
compress = True,
appendScriptToExe = True,
appendScriptToLibrary = True,
icon = 'Icon.ico'
)
setup(
name = "MyProgram",
version = "1.0.0",
description = 'Description',
author = "Joe Bloggs",
author_email = "123#gmail.com",
options = {"build_exe": {"excludes":excludes,"packages":packages,
"include_files":includefiles}},
executables = [exe]
)
This is the error I'm getting.
It looks like it is having trouble finding methods that belong to docx, but my source code calls import docx and it is listed as a dependent module in the setup file so I'm not sure why they aren't being included.
After a LOT of messing about I've finally cracked this. The docx module is dependent on lxml. Even though the raw .py file runs perfectly fine with just docx imported, when cx_freezing you need to explicitly state the dependency by adding lxml to the packages.
At present I am using pyinstaller for bundling my python application. I am equally migrating to pyGObject (due to pygtk being depreciated).
Now pyinstaller does not support pyGObject and I have as of yet not figured out the required hooks... One of the other downsides of pyinstaller is how it bundles into a single executable - it causes the company installed virus scanner to check quite intensively every time the exe is run ==> quite slow startup.
Looking into using cx_freeze due to the pyGObject & py3 support I note it does not have a single-executable option. That in itself isn't an issue if the working directory can be cleaned up, be it via the pyd/dll being bundled into a second zip or into a subdirectory.
Searching around (stackoverflow and other sites), it is illuded to that it can be done, but I am not getting the expected results. Any idea#s?
setup.py is based around this one: http://wiki.wxpython.org/cx_freeze
ok solved:
1) setup.py
import sys
from cx_Freeze import setup, Executable
EXE1 = Executable(
# what to build
script = "foo.py",
initScript = None,
base = 'Win32GUI',
targetDir = "dist",
targetName = "foo.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = True,
appendScriptToLibrary = False,
icon = 'foo.ico'
)
setup(
version = "9999",
description = "...",
author = "...",
name = "...",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": sys.path,
"append_script_to_exe":False,
"build_exe":"dist/bin",
"compressed":True,
"copy_dependent_files":True,
"create_shared_zip":True,
"include_in_shared_zip":True,
"optimize":2,
}
},
executables = [EXE1]
)
2) foo.py header:
import os
import sys
if getattr(sys,'frozen',False):
# if trap for frozen script wrapping
sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin'))
sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin\\library.zip'))
os.environ['TCL_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tcl')
os.environ['TK_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tk')
os.environ['MATPLOTLIBDATA'] = os.path.join(os.path.dirname(sys.executable),'bin\\mpl-data')
I want to create a .exe file. I'm using Python 2.7.3 with wxPython for the GUI. I've installed py2exe for Python 2.7 and tried to create a .exe file following the tutorial at http://www.py2exe.org/index.cgi/Tutorial
When I try to run my created .exe file, I get following error:
File "wx\_gdi.pyc",line823, in BitmapFromImage wx._core.PyAssertionError:
C++ assertion "image.OK()" failed at ..\..\src\msw\bitmap.cpp(802) in
wxBitmap::CreateFromImage(): invalid image
So I looked into my code and the following line is causing the problem:
self.bmpSun = wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(wx.Image('images/sun.gif', wx.BITMAP_TYPE_ANY)), pos = (0,0))
When I browse to the source folder and run the main.py file myself, my app runs fine. I haven't found any help online so far. Can anybody solve this problem/suggest reliable alternatives for py2exe? Thank you.
The line that errors out is looking for an image in the Images folder. That's a path relative to the .exe file created by py2exe. So you need to be sure that that folder exists in correct position relative to the exe, and that it is populated with the images you are going to use. You can do this 2 ways. Either copy the folder to where the exe will reside, or use the data_files keyword arg in the script that makes the .exe. Here's the pertinent part of one of my setup scripts, showing a data_files list of tuples and use of the data_files keyword arg later:
data_files = [('Images', glob('Images/*.*')),
]
includes = ['win32com.decimal_23', 'datetime']
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter', 'unittest']
packages = []
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
'tk84.dll','MSVCP90.dll']
setup(
data_files = data_files,
options = {"py2exe": {"compressed": 2,
"optimize": 2,
"includes": includes,
"excludes": excludes,
"packages": packages,
"dll_excludes": dll_excludes,
"bundle_files": 1,
"dist_dir": "dist",
"xref": False,
"skip_archive": False,
"ascii": False,
"custom_boot_script": '',
}
},
zipfile = None,
windows = [filename]
)