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.
Related
I am trying to create an executable for a script I have already written. It will be used by a coworker who doesn't have python on their machine, so I want to create an executable to make their life easier. I'm currently just trying to get py2exe to work and create an executable for a simple script that simply prints hello world and the constant e from the math module (just to get an idea for how py2exe works).
My setup script is as follows:
from distutils.core import setup
import py2exe
import math
setup(
console=[{'script':'hello.py'}],
options={
'py2exe':{
'packages' : ['math'],
'bundle_files':1
},
},
version='1.0.0'
)
where hello.py is simply:
import math
print('hello world!')
print(f'Here is e: {math.exp(1)}')
The error comes from this stack after I call "python setup.py py2exe" in cmd
Traceback (most recent call last):
File ".\setup.py", line 5, in <module>
setup(
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\core.py", line 148, in setup
return run_commands(dist)
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\core.py", line 163, in run_commands
dist.run_commands()
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\dist.py", line 967, in run_commands
self.run_command(cmd)
File ".\Anaconda3\lib\site-packages\setuptools\dist.py", line 1214, in run_command
super().run_command(command)
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\dist.py", line 986, in run_command
cmd_obj.run()
File ".\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 192, in run
self._run()
File ".\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 273, in _run
builder.build()
File ".\Anaconda3\lib\site-packages\py2exe\runtime.py", line 250, in build
self.build_archive(exe_path)
File ".\Anaconda3\lib\site-packages\py2exe\runtime.py", line 490, in build_archive
base = dist_path.rsplit('\\', 1)[0]
AttributeError: 'NoneType' object has no attribute 'rsplit'
To covert a .py file to .exe you need to install auto-py-to-exe
Windows
pip install auto-py-to-exe
After it is installed, open cmd then type
auto-py-to-exe
Then specify the path of the script and the location where you want to save the .exe file.
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 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.
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!