美好的一天,
我无法在我正在转换为.exe的代码上使用cx_Freeze。
当我运行cx_Freeze时,我得到以下ImportError,没有没有名为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'
我可以确认我的系统上安装了Scipy 0.16,当我将其导入其他python代码时可以正常工作。我目前在Windows上运行python 3.4。以下是我的cx_Freeze的setup.py文件。
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
)
以下是我的主要脚本的进口部分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
如果有人有任何想法为什么cx_Freeze无法找到scipy请告诉我。我试图将文件路径添加到include_files下的scipy,但它没有任何区别。
亲切的问候,
Jonnyishman
答案 0 :(得分:16)
我有完全相同的问题。在这里找到解决方案: https://bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with
在cx_freeze文件夹中找到hooks.py文件。将finder.IncludePackage(“scipy.lib”)中的第548行更改为finder.IncludePackage(“scipy._lib”)。
将“scipy”条目保留在包中,并删除include_files中的“C:\ Python34 \ Lib \ site-packages \ scipy”。
答案 1 :(得分:0)
对于所有与Scipy相关的问题,只要将它们包含在脚本中即可得到解决。它为我工作。请参考我的工作脚本 (注意:此脚本没有tkinter这样的UI库)
此脚本从配置文件中获取数据,并返回加到工作目录中文件中的两个加号。
文件夹结构
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')
生成exe文件的命令
''' make sure to run the below command from working directory where the setup.py file is present.'''
python setup.py build
将使用main.exe文件创建所有必需的二进制文件的构建文件夹。
注意:将config.ini文件放在exe文件夹中,以便exe可以访问该配置文件并产生输出。
答案 2 :(得分:0)
不幸的是,我仍然没有回复要发表评论,但是对于操作程序出现“ No module named scipy.spatial.ckdtree”错误的问题,我只需将“ cKDTree.cp37-win_amd64”重命名为“ ckdtree”即可解决.cp37-win_amd64”位于scipy \ spatial文件夹下。我遇到了类似的问题,就是到处都是用大写字母导入图书馆。