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!
Related
I have created a python application that I now want to make into a standalone application. I am using cx_freeze from a virtualenv, which has worked before on my machine with scripts that have less dependencies.
I am currently running into an issue where cx_freeze errors when it tries to copy dylib files that are named the same to the build folder.
I am running the command:
python3 setup.py build
My setup.py file looks like:
from cx_Freeze import setup, Executable
build_options = {'packages': ["PySimpleGUI", "matplotlib", "pandas", "descartes", "geopandas", "cities_coordinates"],
#'excludes': ["PIL", "pyproj", "setuptools"],
"zip_exclude_packages": [],}
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('main.py', base=base, targetName = 'data_visualizer')
]
setup(name='data_visualizer',
version = '0.1',
description = 'Data visualization tool',
options = {'build_exe': build_options},
executables = executables)
This is the error I am getting:
...
copying /Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libproj.12.dylib -> build/exe.macosx-10.9-x86_64-3.7/libproj.12.dylib
copying /Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libjson-c.2.dylib -> build/exe.macosx-10.9-x86_64-3.7/libjson-c.2.dylib
Traceback (most recent call last):
File "setup.py", line 18, in <module>
executables = executables)
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/dist.py", line 392, in setup
distutils.core.setup(**attrs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/dist.py", line 260, in run
freezer.Freeze()
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 788, in Freeze
self._WriteModules(fileName, self.finder)
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 712, in _WriteModules
relativeSource=True,
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 231, in _CopyFile
resolvedPath=dependent_file
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 231, in _CopyFile
resolvedPath=dependent_file
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 173, in _CopyFile
sourcePath=normalizedSource, targetPath=normalizedTarget
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/darwintools.py", line 446, in getDarwinFile
raise DarwinException(exceptionString)
cx_Freeze.darwintools.DarwinException: Attempting to copy two files to "build/exe.macosx-10.9-x86_64-3.7/libwebp.7.dylib"
source 1: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/PIL/.dylibs/libwebp.7.dylib" (real: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/PIL/.dylibs/libwebp.7.dylib")
source 2: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libwebp.7.dylib" (real: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libwebp.7.dylib")
(This may be caused by including modules in the zip file that rely on binary libraries with the same name.)
If I exclude libraries that introduce a dylib that conflicts with another (see commented out excludes build option in the setup.py) then the command will run successfully, but my built application will fail because of missing dependencies.
Does anyone know of a way to get around this issue?
Some other info if it matters ->
Operating System: Mac OS Big Sur
Python Version: 3.7.8
cx_freeze version: 6.2
This same name error appears to have been reported in this cx_Freeze issue, introduced by this pull request that raises an error when copying a second file onto the path of another.
A workaround was merged to print a warning and simply use the first copy of the file rather than raising an error. As it doesn't appear to have been released yet (as of 2021-03-09, v6.5.3), I installed cx_Freeze from Git (after cloning and entering the repo, I installed it by pip install -e .), ran cxfreeze, and saw warnings such as this:
*** WARNING ***
Attempting to copy two files to '../distcx/libopenblas.0.dylib'
source 1: '/venv/lib/python3.6/site-packages/numpy/.dylibs/libopenblas.0.dylib' (real: '/venv/lib/python3.6/site-packages/numpy/.dylibs/libopenblas.0.dylib')
source 2: '/venv/lib/python3.6/site-packages/scipy/.dylibs/libopenblas.0.dylib' (real: '/venv/lib/python3.6/site-packages/scipy/.dylibs/libopenblas.0.dylib')
(This may be caused by including modules in the zip file that rely on binary libraries with the same name.)
Using only source 1.
where the conflicting file was simply skipped, allowing the build to proceed.
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.
Objective (End-Goal):
I want to create a stand-alone executable Python script (only one file) that includes NumPy and SciPy dependencies for my application.
Background:
From my understanding, to create an executable script in Python - there are three options that are available:
PyInstaller
Py2exe
CxFreeze
I went ahead and tried Py2exe for my development. It appears that CxFreeze does not support the single-file option (from the documentation here). I also considered the option of using PyInstaller, but ran into issues regarding missing DLLs (similar to what is found here). The issue continued to persist even after installing the Microsoft Visual C++ 2010 Redistributable Package in my laptop.
I followed the tutorial to use Py2exe here and was able to get a dummy script executable (Hello World!) working. However, I tried to re-modify the setup.py script specific to my application to include numpy and scipy dependencies (see below):
from distutils.core import setup
import py2exe,sys,numpy,scipy
sys.argv.append('py2exe')
setup(
console=['Application.py'],
options={
'py2exe': {
'includes':['numpy','scipy','scipy.integrate','scipy.special.*','scipy.linalg.*'],
'bundle_files':1,
'compressed':True
}
},
zipfile=None)
This is the resulting error I received when I tried running the script:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\pydev_run_in_console.py", line 52, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/prest/PycharmProjects/Application/setup.py", line 15, in <module>
zipfile=None
File "C:\Python34\lib\distutils\core.py", line 149, 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:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
self._run()
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
builder.analyze()
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\runtime.py", line 164, in analyze
mf.import_hook(modname)
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
module = self._gcd_import(name)
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
return self._find_and_load(name)
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\mf3.py", line 337, in _find_and_load
raise ImportError(name)
ImportError: scipy.linalg.*
These are the versions that I am using relevant for my application:
Python 3.4
NumPy 1.14.5
SciPy 1.1.0
Question:
Can anyone provide any insight as to why I am receiving this error and any next steps to address this? I appreciate any input!
Thanks,
Preston
Closing - went ahead and use PyInstaller for the single-file executable. I re-modified my script to address specific dependencies (only used NumPy).
I am trying to build my wxPython application with Python 3 using py2app. Before I updated my code to Python 3, I was able to do this successfully in Python 2.
After fixing a few initial problems (had to sys.setrecursionlimit(2000) and downgrade to Python 3.5), I got stuck here:
Traceback (most recent call last):
File "setup_pmag_gui.py", line 23, in <module>
setup_requires=['py2app'],
File "/Users/****/anaconda/lib/python3.5/distutils/core.py", line 148, in setup
dist.run_commands()
File "/Users/****/anaconda/lib/python3.5/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/Users/****/anaconda/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 757, in run
self._run()
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 967, in _run
self.run_normal()
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 1075, in run_normal
self.create_binaries(py_files, pkgdirs, extensions, loader_files)
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 1385, in create_binaries
mm.mm.run_file(runtime)
File "/Users/****/my_project/.eggs/macholib-1.8-py3.5.egg/macholib/MachOGraph.py", line 83, in run_file
ValueError: '/Users/****/anaconda/lib/libpython3.5.dylib' does not exist
/Users/****/anaconda/lib/libpython3.5m.dylib does exist, though. Is there a way to tell MachOGraph how to find this?
I am on OS X 10.12.4, using Anaconda Python 3.5. My version of py2app is 0.13.
This is what my setup.py file looks like:
from setuptools import setup
import sys
import os
directory = os.getcwd()
sys.setrecursionlimit(3000)
APP = ['programs/pmag_gui.py']
DATA_FILES = [('', ['dialogs/help_files', 'pmagpy/data_model', 'programs/images'])]
OPTIONS = {'argv_emulation': False,
'iconfile': os.path.join(directory, 'programs', 'images', 'text_x_xslfo.icns')}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
After reading the Tweaking your Info.plist py2app documentation, and this question, I tried adding to OPTIONS:
'plist': dict(PyRuntimeLocations=['/Users/****/anaconda/lib/libpython3.5m.dylib'])
But I get the same exact error message as before.
Any suggestions on how to solve this, or what documentation to look at?
I had same problem with miniconda and py2app. I could fix it by creating a symlink!
ln -s /path/to/lib/libpython3.5m.dylib /path/to/lib/libpython3.5.dylib
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.