【问题标题】:Can't (.exe) my 300 line program using Cx_freeze or PyInstaller. (Matplotlib/Scipy)无法使用 Cx_freeze 或 PyInstaller (.exe) 我的 300 行程序。 (Matplotlib/Scipy)
【发布时间】:2014-01-01 17:10:55
【问题描述】:

我创建了我的第一个 Python 工作程序,并在 PyCharm 中运行。

该程序有一个 GUI,它基本上会弹出一个窗口,有一个目录树,因此用户可以选择一个 jpg 文件,然后将图像二值化,然后让用户使用滑块选择二进制阈值。它还显示了我对其执行图像转换的另外两个图像。

所以,我的问题是我根本无法将其构建为 .exe。我试过 Py2Exe、PyInstaller 和 CX_freeze。我尝试在 Pyinstaller 中构建自己的 .spec 文件,但都没有成功。

我正在运行 2.7.6。我最初安装了 64 位 Python,所以我卸载了所有与 Python 相关的东西并安装了 32 位 Python。

最初,当我使用 pyinstaller 时,我只会得到 ImportError: %1 is not a valid Win32 application,但是自从现在构建 .spec 文件后,我得到了实际错误。我尝试使用钩子(hiddenimports=["wx", "scipy", "skimage", "matplotlib", "numpy"])来确保包含所有正确的文件,但仍然没有成功。

我现在收到的主要错误是:文件“_ufuncs.pyx”,第 1 行,在 init scipy.special._ufuncs(scipy\special_ufuncs.c:19992) ImportError: No module named _ufuncs_cxx

我还在下面创建了一个较小的代码,它只是弹出一个带有绘图的 Wx 窗口,但我在那里收到相同的错误。

就像我提到的,我是 Python 的新手,我读了很多书,但无法弄清楚这一点。我大概花了 10-20 个小时才试图让它正确编译。

下面不是我的实际程序,而是一个使用 wxPython 和 MatPlotLib 的小 sn-p,它会产生相同的错误

这是示例代码:

import wx
from PIL import Image
import matplotlib
matplotlib.use("WXAgg")
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import     FigureCanvasWxAgg as FigCanvas#,NavigationToolbar2WxAgg as NavigationToolbar
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy
from scipy import interpolate
from matplotlib.backends.backend_wx import NavigationToolbar2Wx


from skimage.filter.rank import entropy
from skimage.morphology import disk
from skimage.util import img_as_ubyte
from skimage import color
from skimage import io
import skimage

from numpy import arange, sin, pi

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

    def draw(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    fr = wx.Frame(None, title='test')
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()

这是我收到的错误:

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "c:\python27_32bit\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstalle\loader\pyi_importers.py", line 270, in
 load_module
    exec(bytecode, module.__dict__)
  File "C:\users\iolvera\PycharmProjects\EL and Grayscale Analyzer\build\compilertest\out00-PYZ.pyz\scipy.interpolate",line 156, in <module>
  File "c:\python27_32bit\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\users\iolvera\PycharmProjects\EL and Grayscale Analyzer\build\compilertest\out00-PYZ.pyz\scipy.interpolate.interpolate", line 12, in <module>
  File "c:\python27_32bit\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_module 
   exec(bytecode, module.__dict__)
  File "C:\users\iolvera\PycharmProjects\EL and Grayscale Analyzer\build\compilertest\out00-PYZ.pyz\scipy.special", line 531, in <module>
  File "c:\python27_32bit\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 409, in load_module
    module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
  File "_ufuncs.pyx", line 1, in init scipy.special._ufuncs (scipy\special\_ufuncs.c:19992)
ImportError: No module named _ufuncs_cxx

我的 Python 路径是正确的 C:\Python27_32bit\ 并且我也正确包含了 \lib\site-packages\ 和 \DLL。

就像我提到的,这两个程序在 PyCharm 中都能正常运行。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 问题来自 numpy c-extensions。希望这会为您指明正确的方向。

标签: python-2.7 matplotlib scipy pyinstaller cx-freeze


【解决方案1】:

我遇到了这个问题,我在 py2exe 中修复了它,特别告诉它包含有问题的模块。所以,在setup.py

includes = ['scipy.special._ufuncs_cxx']

setup(...,
      options={"py2exe":{"includes":includes}}
     )

我在其他几个 SciPy 模块中也遇到过这种情况,所以我的 includes 列表中大约有六件事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-12
    • 2018-12-18
    • 1970-01-01
    • 2016-06-14
    • 2013-06-10
    • 1970-01-01
    • 2016-12-26
    • 2014-05-30
    相关资源
    最近更新 更多