Cx_freeze exe failing to completely import docx - python

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.

Related

Python program fails after build with cx_Freeze in Windows

I have a Windows desktop Python 3.9 program using wx and ObjectListView for user forms interaction.
When I run the program inside VSCode all is working fine.
Running the exe after building with cx_Freeze, the program fails on this line:
for attr in self.record.__table__.columns._data.keys():
This is the Class up to this line:
class AddModForm(wx.Dialog):
"""
Opens a single-record form for add/update of record.
dict is a dictionary of the object to work on (defined in globalvariables.py).
"""
def __init__(self, targetObject, olv_dict, row=None, titlePrefix="Add", titleLabel="Record", addRecord=True):
"""Constructor"""
wx.Dialog.__init__(self, None, title="{} {}".format(titlePrefix, titleLabel))
self.targetObject = targetObject
self.olv_dict = olv_dict
self.addRecord = addRecord
self.selectedRow = row
lblSize = (80,-1)
txtSize40 = (40, -1)
txtSize60 = (60, -1)
txtSize80 = (80, -1)
recordToUpdate = {}
if self.selectedRow: # if updating - fetch record
self.recordId = self.selectedRow.id
self.record = forms_controller.getRecordByKey(targetObject, {"id": self.recordId})
# arrange record attributes in a dictionary:
for attr in self.record.__table__.columns._data.keys():
recordToUpdate.update({attr: getattr(self.record, attr)})
Up to this line, the program runs OK, including opening and interacting through user forms, database interaction etc.
This Class is called following a button click by the user, called in this line (in another Class):
self.modifyForm = form_addmod.AddModForm(Departments, globalvariables.dict_olv_departments, selectedRow, "Update", "Department", False)
As far as the user experience, the button is clicked but the called form just won't open. No error is shown.
For reference, this is the setup.py for the build:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["configparser", "pymysql", "sqlalchemy", "ObjectListView", "validate_email", "wx"],
"include-files": ["etc\configuration.txt", "backup_db"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
includefiles = ['etc\\']
includes = []
excludes = ['Tkinter']
packages = ["configparser", "pymysql", "sqlalchemy", "ObjectListView", "validate_email", "wx"]
setup(
name = 'Employees Management',
version = '1.0',
description = '...',
author = '...',
author_email = 'info#...',
options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [Executable(script="main.py", base=base, icon="media\LAVI.ico", targetName="Employees Management.exe")]
)
This program used to work perfect on Python 3.6.5. I just needed a small fix, and re-installing some libraries again (probably for versions compatibility).
Any idea where can I look for? any known issues with cx_Freeze build process or runtime on Windows with this statement? maybe referencing the Table object?

cx_Freeze help: is there a way to NOT make console open?

I am trying to convert a python game (made with pygame) into a exe file for windows, and I did using cx_Freeze. No problems there.
The thing is that when I launch myGame.exe, it opens the normal Pygame window and a console window(which I do not want).
Is there a way to remove the console window? I read most of the documentation, but I saw nothing really (except base, but I don't get what that is).
BTW, here is my setup file:
import cx_Freeze
exe = [cx_Freeze.Executable("myGame.py")]
cx_Freeze.setup(
name = "GameName",
version = "1.0",
options = {"build_exe": {"packages": ["pygame", "random", "ConfigParser", "sys"], "include_files": [
"images", "settings.ini", "arialbd.ttf"]}},
executables = exe
)
Here's a screen shot of what happens when I launch the exe:
So what was wrong, was that the setup.py file was missing a parameter.
What you need to add is base = "Win32GUI" to declare that you do not need a console window upon launch of the application.
Here's the code:
import cx_Freeze
exe = [cx_Freeze.Executable("myGame.py", base = "Win32GUI")] # <-- HERE
cx_Freeze.setup(
name = "GameName",
version = "1.0",
options = {"build_exe": {"packages": ["pygame", "random", "ConfigParser", "sys"],
"include_files": ["images", "settings.ini", "arialbd.ttf"]}},
executables = exe
)
The parameter can be passed also by the shell if you are making a quick executable
like this:
cxfreeze my_program.py --base-name=WIN32GUI

cx_freeze & bundling files

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')

how can reference from one Eclipse project to another when using cx_freeze

I have a problem
I have eclipse work space it's contain 4project each project reference from anther project. When I need to make an executable file using cx_Freeze, it can't import the other referenced project to it?
thanks
I tried this code:
base = None
from cx_Freeze import setup, Executable
if sys.platform == "win32":
base = "Win32GUI"
exe = Executable(
script="MyForm.py",
base="Win32GUI",packages=['QtCore', 'QtGui', 'QtSvg','tkinter','time','os','cls_MyForm','threading','sys','DAL','FS_Watch_Collection'],
compress=False, copyDependentFiles=False)
setup(
name = "First Version of File Watcher",
version = "1",
description = "File Watcher Program",
executables = [exe])
and this code:
from cx_Freeze import setup, Executable
#http://www.youtube.com/watch?v=XHcDHSWRCRQ
#http://www.python-forum.org/pythonforum/viewtopic.php?f=4&t=34501&hilit=cxfreeze
includefiles = ['README.txt', 'CHANGELOG.txt']
includes = []
excludes = ['tkinter']
packages = ['pk_LocalStore','QtCore', 'QtGui', 'QtSvg','time','os','threading','sys']
setup(
name = 'First Version of File Watcher',
version = '0.1',
description = 'File Watcher Program',
author = 'ITE CO',
author_email = 'info#from-masr.com',
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [Executable('MyForm.py')]
)
but it's not working correctly

cx_Freeze missing modules error

I am new to Python and cx_Freeze. Please help me to get it working.
And run the command:
python setup.py build
It is giving me the following error.
Missing modules:
? System imported from serial.serialcli
? TERMIOS imported from serial.serialposix
? clr imported from serial.serialcli
? wx imported from wxversion
I am using the following setup.py file.
# Let's start with some default (for me) imports...
from cx_Freeze import setup, Executable
# Process the includes, excludes and packages first
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter']
packages = []
path = []
GUI2Exe_Target_1 = Executable(
# what to build
script = "..\esp\main.py",
initScript = None,
base = 'Win32GUI',
targetDir = r"dist",
targetName = "acup_new.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
setup(
version = "0.1",
description = "No Description",
author = "No Author",
name = "cx_Freeze Sample File",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": path
}
},
executables = [GUI2Exe_Target_1]
)
Based on this question, it seems like you may need to add these modules to the includes[] list in your setup.py file.
I don't recall having to do this when using cx_Freeze, but I'll edit this answer once I can find more information.

Categories

Resources