Cx_freeze导入错误:没有名为scipy的模块。

7

大家好,

我在将一份代码转换成 .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,请告诉我。我尝试将scipy的文件路径添加到include_files中,但没有任何改变。
祝好,
Jonnyishman
3个回答

17

干得好!我做了这个更改,现在它完美地运作了。你是个明星。 - Jonnyishman
5
在这里建议更改hooks.py可以解决问题,但我不明白为什么。为什么这个解决方案有效? - Puggie
你的解决方案没有起作用,它抛出了一个错误,因为没有名为sys<for module collections.sys>的文件。在..\cx_Freeze\freezer.py的第762行。 - Jesh Kundem
1
这个解决方案还解决了我遇到的No module named scipy.spatial.ckdtree问题,即使在应用了这里setup.py文件修复后仍然存在。 - S3DEV
此外,对于 cx_freeze v5.0.2,它在第564行。 - S3DEV

1
如果您将Scipy相关问题包含在脚本中,则所有问题都将得到解决。这对我有用。请参考我的工作脚本(注意:此脚本不包含任何UI库,如tkinter)。
此脚本从配置文件获取数据,并返回写入工作目录文件中的两个数字的总和。 文件夹结构

enter image description here

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

构建文件夹将创建主exe文件和所有必需的二进制文件。
注意:将config.ini文件放置在exe文件夹中,以便exe可以访问配置文件并生成输出。

enter image description here


谢谢。那真的帮助了我解决 pyttsx3 模块的错误。 - LetzerWille

0
很不幸,我仍然没有足够的声望来发表评论,但对于遇到“无模块名为scipy.spatial.ckdtree”的错误的op,我通过将“cKDTree.cp37-win_amd64”重命名为“scipy\spatial”文件夹下的“ckdtree.cp37-win_amd64”来解决了这个问题。我也曾遇到一些库被引入时用大写字母的类似问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接