[英]cx_Freeze 5.0: ImportError: No module named 'scipy.__config__'
[英]Cx_freeze ImportError no module named scipy
大家好,
我在正在转换为 .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 中,但没有任何区别。
亲切的问候,
约翰尼什曼
我有完全相同的问题。 在这里找到解决方案: https : //bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with
在 cx_freeze 文件夹中找到 hooks.py 文件。 将第 548 行从 finder.IncludePackage("scipy.lib") 更改为 finder.IncludePackage("scipy._lib")。
保留包中的“scipy”条目并删除 include_files 中的“C:\\Python34\\Lib\\site-packages\\scipy”。
对于所有与 Scipy 相关的问题,如果您将它们包含在脚本中,就会得到解决。 它对我有用。 请参考我的工作脚本(注意:这个脚本没有任何像 tkinter 这样的 UI 库)
此脚本从配置文件中获取数据并返回写入工作目录中文件的加法两个数字。
文件夹结构
设置文件
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
)
主文件
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
build 文件夹使用 main.exe 文件创建,其中包含所有必需的二进制文件。
注意:将 config.ini 文件放在 exe 文件夹中,以便 exe 可以访问配置文件并生成输出。
不幸的是,我仍然没有代表发表评论,但对于“没有名为 scipy.spatial.ckdtree 的模块”错误的问题,我通过简单地将“cKDTree.cp37-win_amd64”重命名为“ckdtree.cp37-”来解决它win_amd64" 在 scipy\\spatial 文件夹下。 我在这里和那里用大写字母导入库时遇到了类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.