Cannot compile with cx_Freeze - python

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.

Related

Cx_freeze ImportError no module named scipy

Good day all,
I am having trouble using cx_Freeze on a code I am working on converting to a .exe.
When I run cx_Freeze I get the following ImportError that there no no module named scipy
running install
running build
running build_exe
Traceback (most recent call last):
File "setup.py", line 25, in <module>
executables = executables
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\install.py", line 539, in run
self.run_command('build')
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\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 232, in run
freezer.Freeze()
File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 619, in Freeze
self.finder = self._GetModuleFinder()
File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 378, in _GetModuleFinder
finder.IncludePackage(name)
File "C:\Python34\lib\site-packages\cx_Freeze\finder.py", line 686, in IncludePackage
module = self._ImportModule(name, deferredImports)
File "C:\Python34\lib\site-packages\cx_Freeze\finder.py", line 386, in _ImportModule
raise ImportError("No module named %r" % name)
ImportError: No module named 'scipy'
I can confirm that I have Scipy 0.16 installed on my system which works when I import it into other python code. I am currently running python 3.4 on Windows. The following is my setup.py file for cx_Freeze.
import cx_Freeze
import sys
import matplotlib
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [cx_Freeze.Executable('fractureGUI.py', base=base, icon='star_square.ico')]
packages = ['tkinter','matplotlib','scipy']
include_files = ['star_square.ico', 'C:\\Python34\\Lib\\site-packages\\scipy']
cx_Freeze.setup(
name = 'FracturePositionMonteCarlo',
options = {'build_exe': {'packages':packages,
'include_files':include_files}},
version = '0.01',
description = 'Fracture Depth Monte Carlo',
executables = executables
)
The following is the import section of my main script, fractureGUI.py.
import scipy
from random import random
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import style
style.use('ggplot')
import tkinter as tk
from tkinter import ttk, filedialog
import sys
import json
If anybody has any ideas why cx_Freeze is unable to find scipy please do let me know. I tried to add the filepath to scipy under include_files but it made no difference.
Kind regards,
Jonnyishman
I had exactly the same issue. Found the solution here:
https://bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with
Find the hooks.py file in cx_freeze folder. Change line 548 from finder.IncludePackage("scipy.lib") to finder.IncludePackage("scipy._lib").
Leave the "scipy" entry in packages and delete 'C:\Python34\Lib\site-packages\scipy' in include_files.
For all the Scipy related issues gets resolved if you include them in the script. It worked for me. Please refer my working script
(Note: this script doesnot have any UI library like tkinter)
This scripts fetches the data from config file and returns the addition two number which is get written in the file in the working directory.
FolderStructure
setup.py
import sys
import cx_Freeze
from cx_Freeze import setup, Executable
from scipy.sparse.csgraph import _validation
import scipy
import matplotlib
'''Include the package for which you are getting error'''
packages = ['matplotlib','scipy']
executables = [cx_Freeze.Executable('main.py', base='Win32GUI')]
'''include the file of the package from python/anaconda installation '''
include_files = ['C:\\ProgramData\\Continuum\\Anaconda\\Lib\\site-packages\\scipy']
cx_Freeze.setup(
name = 'Test1',
options = {'build_exe': {'packages':packages,
'include_files':include_files}},
version = '0.1',
description = 'Extraction of data',
executables = executables
)
main.py
import os, numpy as np
import configparser
from helper_scripts.help1 import Class_A
path = os.path.dirname(os.path.abspath('__file__')) + '\\'
conf = configparser.ConfigParser()
conf.read(path + 'config.ini')
a = eval(conf.get('inputs','input_1'))
b = eval(conf.get('inputs','input_2'))
obj = Class_A()
res = obj.getData(a,b)
if not os.path.exists(path + 'Result.txt'):
with open(path + 'Result.txt', 'w', encoding ='utf-8') as f:
f.write(f'result is : {str(res)}\n')
else:
with open(path + 'Result.txt', 'a', encoding ='utf-8') as f:
f.write(f'result is : {str(res)}\n')
Command to generate the exe file
''' make sure to run the below command from working directory where the setup.py file is present.'''
python setup.py build
The build folder gets created with main.exe file all the required binaries files.
Note: Place the config.ini file in the exe folder so that exe can access the config file and produce the output.
Unfortunately I still don't have rep to comment, but for the op having issues with the "No module named scipy.spatial.ckdtree" error, I solved it by simply renaming "cKDTree.cp37-win_amd64" to "ckdtree.cp37-win_amd64" under scipy\spatial folder. I had similar issues with libraries being imported with capital letters here and there.

Python cx_freeze 4.3.4: Setting targetName causes errors

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
)

cx_Freeze: cannot find file/directory icon.gif

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.

cx_freeze and pycrypto is missing modules?

Here is my setup.py file for Python 3.3:
#/usr/bin/env python3
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"packages": [
"os","io","copy","struct","hashlib","random",
"urllib","pycurl","json","Crypto"
],
"includes": [ "urllib.parse", ],
"excludes": ["tkinter"],
"icon":"backup.ico"
}
setup( name = "BlindBackup",
version = "1.0",
description = "BlindBackup client",
options = {"build_exe": build_exe_options},
executables = [Executable("backup.py", base=None)])
I can execute "py -3 setup.py build_exe" but the exe won't work. By starting the generated backup.exe I get this error message:
ImportError: No module named 'Crypto.Cipher'; Crypto is not a package
However, Crypto is a package! I have also tried to add these into the includes section:
"includes": ["urllib.parse",
"Crypto","Crypto.Cipher","Crypto.Cipher.AES",],
But then I cannot even build the exe:
File "C:\Python33\lib\site-packages\cx_Freeze\dist.py", line 362, 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\site-packages\cx_Freeze\dist.py", line 232, in run
freezer.Freeze()
File "C:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 603, in Freeze
self.finder = self._GetModuleFinder()
File "C:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 343, in _GetMouleFinder
finder.IncludeModule(name)
File "C:\Python33\lib\site-packages\cx_Freeze\finder.py", line 678, in IncludeModule
namespace = namespace)
File "C:\Python33\lib\site-packages\cx_Freeze\finder.py", line 386, in _ImportModule
raise ImportError("No module named %r" % name)
ImportError: No module named 'Crypto.Cipher'
Which makes no sense, because there is a module named Crypto.Cipher.
You can test the same setup.py script with python 3 - just create a backup.py script and put this inside:
from Crypto.Cipher import AES
It has been suggested that I install precompiled voidspace modules ( see Error executing the result of cx_freeze using pycrypto ) but it doesn't work either. I did not want to write comment to a 7 month old question, maybe that is what I should have done? Anyway, I have this problem now and I cannot fix this on my own. Please help me!
Okay, I was silly. I have created a module in my project called "crypto.py". It is true that this module was a different one under Linux. However, under Windows, the package "Crypto" and the module "crypto" appeared to be the same. cx_Freeze confused them, and tried to find Cipher module under the crypto.py "packacge" which was a module instead.
Refactored my module to a different name and now it works!

Error converting .py to executable using cx_freeze

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"],}

Categories

Resources