Cx_freeze ImportError 没有名为 scipy 的模块

Cx_freeze ImportError no module named scipy

大家好,

我在使用 cx_Freeze 处理正在转换为 .exe 的代码时遇到问题。

当我 运行 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 代码时它可以工作。我目前 运行正在 python 3.4 Windows。以下是我的 setup.py 文件 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
    )

以下是我的主脚本的导入部分,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 库)

此脚本从配置文件中获取数据,并returns在工作目录中的文件中写入另外两个数字。

文件夹结构

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 可以访问配置文件并生成输出。

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