I am very new to cx_freeze and I am trying to understand it a bit better, I have 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"]}
setup( name = "guifoo",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("mypy.py", base="Console", targetName="hello")])
which if I remove the targetName="hello" it works however when I include it, it doesnt. Would anyone know why?
This is my python code:
# encoding: utf8
import math
print "Starting..."
print math.sqrt(16)
input("please press enter to exit...")
After running python setup.py build I get the following errors:
running build
running build_exe
creating directory build\exe.win32-2.7
copying C:\Python27\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.win32-2.7\hello
copying C:\Windows\system32\python27.dll -> build\exe.win32-2.7\python27.dll
Traceback (most recent call last):
File "setup.py", line 11, in <module>
executables = [Executable("mypy.py", base="Console", targetName="hello")])
File "C:\Python27\lib\site-packages\cx_Freeze\dist.py", line 362, in setup
distutils.core.setup(**attrs)
File "C:\Python27\lib\distutils\core.py", line 151, in setup
dist.run_commands()
File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
self.run_command(cmd)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "C:\Python27\lib\distutils\command\build.py", line 127, in run
self.run_command(cmd_name)
File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "C:\Python27\lib\site-packages\cx_Freeze\dist.py", line 232, in run
freezer.Freeze()
File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 621, in Freeze
self._FreezeExecutable(executable)
File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 211, in _FreezeExecutable
self._AddVersionResource(exe.targetName)
File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 150, in _AddVersionResource
stamp(fileName, versionInfo)
File "C:\Python27\lib\site-packages\win32\lib\win32verstamp.py", line 159, in stamp
h = BeginUpdateResource(pathname, 0)
pywintypes.error: (2, 'BeginUpdateResource', 'The system cannot find the file specified.')
Adding a .exe at the target name does resolve this
Reposting as an answer:
targetName is the filename of the executable it's going to produce. On Windows, executables must have a .exe extension, so you'll need to set it as 'hello.exe' rather than just 'hello'.
I ran into this problem with the latest version of Cx_freeze.
I found that I needed to change my Executable call in the setup.py to use a relative path for the dist directory.
Changes needed in setup.py
From
MyExe_Target_1 = Executable(
# what to build
script = "main.py",
initScript = None,
base = None,
targetDir = r"dist",
targetName = "MyWindowsApp.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
To:
MyExe_Target_1 = Executable(
# what to build
script = "main.py",
initScript = None,
base = None,
targetDir = r".\\dist", # needs in Windows format relative to the working dir!
targetName = "MyWindowsApp.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
Related
I'm trying to compile python script to exe.
My script - hello.py:
print("hello")
My setup.py:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
name = 'hello',
description = 'hello script',
version = '1.0',
options = {'py2exe': {'bundle_files': 1, 'compressed': True,'dist_dir': ".",'dll_excludes':['w9xpopen.exe']}},
console = [{'script': r"hello.py"}],
zipfile = None,
)
I run:
py -3 setup.py install
The error:
py -3 setup.py install
running install
running build
running install_egg_info
Removing C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\hello-1.0-py3.7.egg-info
Writing C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\hello-1.0-py3.7.egg-info
running py2exe
Traceback (most recent call last):
File "setup.py", line 19, in <module>
zipfile = None,
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
self._run()
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
builder.analyze()
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\runtime.py", line 160, in analyze
self.mf.import_hook(modname)
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
module = self._gcd_import(name)
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
return self._find_and_load(name)
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 357, in _find_and_load
self._scan_code(module.__code__, module)
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 388, in _scan_code
for what, args in self._scan_opcodes(code):
File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 417, in _scan_opcodes
yield "store", (names[oparg],)
IndexError: tuple index out of range
Do you know how to resolve this error?
py2exe seems to support up to Python 3.4 (thanks Michael Butscher)
However, there are other libraries such as Pyinstaller which work just fine, and are compatible with a variety of Python versions (from Python 2.7 to 3.5+)
Check it out, it's actually really easy :)
https://pyinstaller.readthedocs.io/en/stable/
I made a program which worked fine and now I tried to compile it with cx_Freeze but got TypeError: can only concatenate list (not "NoneType") to list error. So how can I fix this problem so that I can compile my program correctly to .exe
My configuration: python 2.7, cx_Freeze 5.1.1
My program contains following modules: os, time, string, random, smtplib, _winreg, requests, pyautogui, subprocess, email, SimpleCV
My setup file code:
import sys
from cx_Freeze import setup, Executable
company_name = 'My own company'
product_name = 'Program'
sys.setrecursionlimit(5000)
bdist_msi_options = {
'add_to_path': False,
'initial_target_dir': r'[C:\Program Files (x86)]\%s\%s' % (company_name, product_name),
}
path = sys.path
build_exe_options = {
"path": path,
"icon": "myicon.ico"}
base = None
if sys.platform == "win32":
base = "Win32GUI"
exe = Executable(script='My_program.py',
base=base,
icon='myicon.ico',
)
setup(name = "My program",
version = "1.1",
description = "This is my first program",
executables = [exe],
options = {'bdist_msi': bdist_msi_options})
Error:
Traceback (most recent call last):
File "setup.py", line 33, in <module>
options = {'bdist_msi': bdist_msi_options})
File "C:\Python27\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
distutils.core.setup(**attrs)
File "C:\Python27\lib\distutils\core.py", line 151, in setup
dist.run_commands()
File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
self.run_command(cmd)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "C:\Python27\lib\distutils\command\build.py", line 127, in run
self.run_command(cmd_name)
File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "C:\Python27\lib\site-packages\cx_Freeze\dist.py", line 219, in run
freezer.Freeze()
File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 623, in Freeze
self._WriteModules(fileName, self.finder)
File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 600, in _WriteModules
path = os.pathsep.join([origPath] + module.parent.path)
TypeError: can only concatenate list (not "NoneType") to list
The solution is placing opencv_ffmpeg342.dll file in the same directory where the executable file is located.
opencv_ffmpeg342.dll is located at [Place where you have installed python] \ Lib \ site-packages \ cv2
The module cv2 causes an infinite recursion with cx_Freeze, see cx_Freeze - opencv compatibility
Remove the statement
sys.setrecursionlimit(5000)
from your setup script. You should then see the following error
module = self._modules[name] = Module(name)
RuntimeError: maximum recursion depth exceeded while calling a Python object
If you can live without using cv2, you can exclude it (see below how to do that).
I guess a further problem could be that SimpleCV requires numpy and scipy, and these packages need to be included explicitly in the cx_Freeze setup script.
Altogether, try to modify your build_exe_options as follows:
build_exe_options = {"path": path,
"include_files": ["myicon.ico"],
"packages": ["numpy", "scipy"],
"excludes": ["scipy.spatial.cKDTree", "cv2"]}
The path option is actually not necessary because the default value is sys.path.
The icon option does not exist, I guess you intended to use include_files. This option might not be necessary if you don't use the icon file in the program itself.
Don't forget to add the build_exe_options to the setupcommand:
setup(name = "My program",
version = "1.1",
description = "This is my first program",
executables = [exe],
options = {'build_exe': build_exe_options,
'bdist_msi': bdist_msi_options})
On my Linux machine, I still get errors caused by matplotlib after these modifications (see cx_freeze error with matplotlib data), but they seem specific to Linux so under Windows it might work for you.
I am using cx_Freeze with Python 3.4.1 and I am trying to create an application from one of my Python programs. Unfortunately, I get this error after running the setup.py build:
cx_Freeze.freezer.ConfigError: cannot find file/directory named icon.gif
Here is my setup file:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32" : base = "Win32GUI"
opts = {"include_files": ['icon.gif', "EndingsPerfect.txt", "EndingsPPP.txt", "LatinEnglishPronouns.txt", "1stConj/", "2ndConj/", "3rdConj/", "4thConj/"], "includes": ["re"]}
setup(name = "LT",
version = "1.0",
description = "Latin verbs",
author = "Laurence vS",
options = {"build_exe": opts},
executables = [Executable("latintranslatewithguipyw.pyw", base = base)])
This error confuses me since the icon.gif file is in the same folder as the setup file.
Here is the full traceback:
C:\Users\Laurence> python "C:\Users\Laurence\Dropbox\Python programs\GUIs\latintr
anslatewithgui\setup.py" build
running build
running build_exe
Traceback (most recent call last):
File "C:\Users\Laurence\Dropbox\Python programs\GUIs\latintranslatewithgui\set
up.py", line 14, in <module>
executables = [Executable("latintranslatewithguipyw.pyw", base = base)])
File "C:\Python34\lib\site-packages\cx_Freeze\dist.py", line 362, in setup
distutils.core.setup(**attrs)
File "C:\Python34\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python34\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python34\lib\distutils\command\build.py", line 126, in run
self.run_command(cmd_name)
File "C:\Python34\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python34\lib\site-packages\cx_Freeze\dist.py", line 231, in run
metadata = metadata)
File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 108, in __init
__
self._VerifyConfiguration()
File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 498, in _Verif
yConfiguration
sourceFileName)
cx_Freeze.freezer.ConfigError: cannot find file/directory named icon.gif
Any help would be appreciated.
Reposting as an answer:
Relative path names are found relative to where you run from, not where the the setup.py script is. Use cd in the terminal to change to the directory where setup.py is, and then run python setup.py build.
If that's not practical for some reason, you could use os.chdir() inside the setup.py script.
When I try to build my game compiler I get error:
Traceback (most recent call last):
File "gamecomp.py", line 22, in <module>
executables = [exe])
File "C:\Python33\lib\site-packages\cx_Freeze\dist.py", line 365, in setup
distutils.core.setup(**attrs)
File "C:\Python33\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python33\lib\distutils\dist.py", line 929, in run_commands
self.run_command(cmd)
File "C:\Python33\lib\distutils\dist.py", line 948, in run_command
cmd_obj.run()
File "C:\Python33\lib\distutils\command\build.py", line 126, in run
self.run_command(cmd_name)
File "C:\Python33\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "C:\Python33\lib\distutils\dist.py", line 948, in run_command
cmd_obj.run()
File "C:\Python33\lib\site-packages\cx_Freeze\dist.py", line 234, in run
metadata = metadata)
File "C:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 104, in __init
__
self.includeFiles = self._ProcessPathSpecs(includeFiles)
File "C:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 384, in _Proce
ssPathSpecs
raise ConfigError("target path for include file may not be "
cx_Freeze.freezer.ConfigError: target path for include file may not be an absolu
te path
My compiler is:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"], "include_files":["C:\Documents and Settings\boot_1.bmp", "C:\Documents and Settings\boot_2.bmp", 'C:\Documents and Settings\boot_3.bmp', 'C:\Documents and Settings\boot_4.bmp', 'C:\Documents and Settings\fish1.bmp', 'C:\Documents and Settings\fish2.bmp', 'C:\Documents and Settings\fish3.bmp', 'C:\Documents and Settings\fish4.bmp', 'C:\Documents and Settings\goldenfish_1.bmp', 'C:\Documents and Settings\goldenfish_2.bmp']}
base = None
if sys.platform == "win32":
base = "Win32GUI"
exe=Executable(
script="game.py",
base=base
)
setup( name = "Game name",
version = "1.0",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [exe])
If I don't use include_files I can compile the game without any errors, but others can't play the game.
How can I compile my game without errors / fix errors?
I get it now.
I have to write: "boot_1.bmp" instead "C:\Documents and Settings\boot_1.bmp" and so for all other pictures.
I also have to change it in the game code.
I am using python 3.3.3
the following is my setup.py code
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "send_email",
version = "0.1",
description = "send the email",
options = {"build_exe": build_exe_options},
executables = [Executable("send_email.py", icon="icon.ico", base=base)])
The only import in my send_email.py file is smtplib.
The following error message is what I receive when building the executable in the command window:
c:\Python33>python.exe setup.py build
running build
running build_exe
copying c:\Python33\lib\site-packages\cx_Freeze\bases\Win32GUI.exe -> build\exe.
win-amd64-3.3\send_email.exe
copying C:\Windows\SYSTEM32\python33.dll -> build\exe.win-amd64-3.3\python33.dll
Traceback (most recent call last):
File "setup.py", line 17, in <module>
executables = [Executable("send_email.py", icon="icon.ico", base=base)])
File "c:\Python33\lib\site-packages\cx_Freeze\dist.py", line 365, in setup
distutils.core.setup(**attrs)
File "c:\Python33\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "c:\Python33\lib\distutils\dist.py", line 917, in run_commands
self.run_command(cmd)
File "c:\Python33\lib\distutils\dist.py", line 936, in run_command
cmd_obj.run()
File "c:\Python33\lib\distutils\command\build.py", line 126, in run
self.run_command(cmd_name)
File "c:\Python33\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "c:\Python33\lib\distutils\dist.py", line 936, in run_command
cmd_obj.run()
File "c:\Python33\lib\site-packages\cx_Freeze\dist.py", line 235, in run
freezer.Freeze()
File "c:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 577, in Freeze
self._FreezeExecutable(executable)
File "c:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 169, in _Freez
eExecutable
cx_Freeze.util.AddIcon(exe.targetName, exe.icon)
SystemError: error return without exception set
I had the same message error and I fixed it by giving the full path of the icon file.
By the way, make sure the icon is in .ico format (At first I renamed the extensions of a .png file to .ico and caused the process to crash, lastly I converted the .png file to the .ico format and it worked).
use this setup file .
from cx_Freeze import setup, Executable
GUI2Exe_Target_1 = Executable(
script = "Your scripts",
initScript = None,
base = 'Win32GUI',
targetName = "app.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = "YOUR ICON FILE.ico"
)
excludes = ["pywin", "tcl", "pywin.debugger", "pywin.debugger.dbgcon",
"pywin.dialogs", "pywin.dialogs.list", "win32com.server",
"email"]
includes = ["PyQt4.QtCore","PyQt4.QtGui","win32gui","win32com","win32api","html.parser","sys","threading","datetime","time","urllib.request","re","queue","os"]
packages = []
path = []
setup(
version = "1.0",
description = "myapp",
author = "me",
author_email = "email#email.com",
name = "Your app name !",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": path
}
},
executables = [GUI2Exe_Target_1]
)
I had the SAME EXACT ERROR that i just fixed right now! There is a very simple solution , you are getting this error becasue of your icon file.
I take it you have a setup.py file with something along the lines of executables = [cx_Freeze.Executable("filename.py", base=base, icon="youricon")]
You need to make sure that your icon is an .ico file. Simply search for .gif or .png to .ico converter and it will do it for you!
Make sure also that your .ico file is inside your folder of your files.
Make sure to include files in the options
You probably have something else in your setup.py along the lines of...
options = {"build_exe":{"packages":["THEMODS YOU IMPORTED HERE"],"include_files":["THE FILES NEED TO BE HERE"]}
This is what fixed the problem for me. LMK if this helps :)
Change your options to:
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"],"include_files": ["icon.ico"],}