【问题标题】:Cx_freeze ImportError no module named scipyCx_freeze ImportError 没有名为 scipy 的模块
【发布时间】:2015-09-07 07:02:56
【问题描述】:

大家好,

在我正在转换为 .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 中,但没有任何区别。

亲切的问候,

强尼什曼

【问题讨论】:

    标签: python scipy cx-freeze


    【解决方案1】:

    我遇到了完全相同的问题。在这里找到解决方案: 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”。

    【讨论】:

    • 干杯人。我做了改变,现在它很有效。你是明星。
    • 按照这里的建议更改hooks.py 可以解决问题,但我不明白如何。为什么这个解决方案有效?
    • 您的解决方案不起作用,它抛出了一个错误,因为没有名为 sys 的文件 Line 762 of ..\cx_Freeze\freezer.py
    • 此解决方案还解决了我遇到的No module named scipy.spatial.ckdtree 问题——即使在应用setup.py 文件修复here 之后也是如此。
    • 另外...对于cx_freeze v5.0.2,它位于第 564 行。
    【解决方案2】:

    对于所有与 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
        )
    

    ma​​in.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 可以访问配置文件并产生输出。

    【讨论】:

    • 谢谢。这确实帮助我解决了 pyttsx3 模块错误。
    【解决方案3】:

    不幸的是,我仍然没有代表发表评论,但是对于遇到“No module named scipy.spatial.ckdtree”错误的操作,我通过简单地将“cKDTree.cp37-win_amd64”重命名为“ckdtree”来解决它.cp37-win_amd64" 在 scipy\spatial 文件夹下。我在这里和那里用大写字母导入库时遇到了类似的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2013-03-11
      • 2014-08-17
      • 2012-12-07
      相关资源
      最近更新 更多