Python py2exe和numpy相处不好

Python py2exe和numpy相处不好,python,numpy,matplotlib,scipy,py2exe,Python,Numpy,Matplotlib,Scipy,Py2exe,我正在尝试使用py2exe-0.6.9.win32来封装我在Python2.6.5中编写的应用程序,该应用程序使用以下具有相关下载文件名的对象库: matplotlib-0.99.3.win32 scipy-0.8.0b1-win32 wxPython2.8-win32-unicode-2.8.11.0 我尝试启动生成的.exe文件时收到错误消息。目前,错误消息与numpy有关,但在此之前,我没有加载matplot lib数据文件,因此阻止了exe文件的启动 我没有发布一英里长的代码和所有错误

我正在尝试使用py2exe-0.6.9.win32来封装我在Python2.6.5中编写的应用程序,该应用程序使用以下具有相关下载文件名的对象库:

matplotlib-0.99.3.win32

scipy-0.8.0b1-win32

wxPython2.8-win32-unicode-2.8.11.0

我尝试启动生成的.exe文件时收到错误消息。目前,错误消息与numpy有关,但在此之前,我没有加载matplot lib数据文件,因此阻止了exe文件的启动

我没有发布一英里长的代码和所有错误消息,而是发布了一个更一般的问题:有人能告诉我一些说明,如何使用py2exe创建一个工作的exe文件,使所有这些对象库和版本能够很好地协同工作吗?

我一直在阅读谷歌搜索出的关于这个主题的文章,但这似乎是一场白费力气的追逐,因为每个人都在使用不同版本的东西。我可以更改其中一些对象库的一些版本,如果这样做会产生影响的话,但是我已经在这个信号处理应用程序中编写了5000行代码,如果可能的话,我宁愿不必重新编写所有代码


编辑:

下面是一个名为GUIdiagnostics.py的文件中代码的简化版本,该文件用于测试py2exe脚本导入实际应用程序中所需的所有库的能力:

import time
import wxversion
import wx
import csv
import os
import pylab as p
from scipy import stats
import math
from matplotlib import *
from numpy import *
from pylab import *
import scipy.signal as signal
import scipy.optimize
import Tkinter

ID_EXIT = 130

class MainWindow(wx.Frame):
    def __init__(self, parent,id,title):
        wx.Frame.__init__(self,parent,wx.ID_ANY,title, size = (500,500), style =     wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)

        # A button
        self.button =wx.Button(self, label="Click Here", pos=(160, 120))
        self.Bind(wx.EVT_BUTTON,self.OnClick,self.button)

        # the combobox Control
        self.sampleList = ['first','second','third']
        self.lblhear = wx.StaticText(self, label="Choose TestID to filter:", pos=(20, 75))
        self.edithear = wx.ComboBox(self, pos=(160, 75), size=(95, -1),     choices=self.sampleList, style=wx.CB_DROPDOWN)

        # the progress bar
        self.progressMax = 3
        self.count = 0
        self.newStep='step '+str(self.count)
        self.dialog = None

        #-------Setting up the menu.
        # create a new instance of the wx.Menu() object
        filemenu = wx.Menu()

        # enables user to exit the program gracefully
        filemenu.Append(ID_EXIT, "E&xit", "Terminate the program")

        #------- Creating the menu.
        # create a new instance of the wx.MenuBar() object
        menubar = wx.MenuBar()
        # add our filemenu as the first thing on this menu bar
        menubar.Append(filemenu,"&File")
        # set the menubar we just created as the MenuBar for this frame
        self.SetMenuBar(menubar)
        #----- Setting menu event handler
        wx.EVT_MENU(self,ID_EXIT,self.OnExit)

        self.Show(True)

    def OnExit(self,event):
        self.Close(True)

    def OnClick(self,event):
        try:
            if not self.dialog:
                self.dialog = wx.ProgressDialog("Progress in processing your data.", self.newStep,
                                            self.progressMax,
                                            style=wx.PD_CAN_ABORT
                                            | wx.PD_APP_MODAL
                                            | wx.PD_SMOOTH)
            self.count += 1
            self.newStep='Start'
            (keepGoing, skip) = self.dialog.Update(self.count,self.newStep)
            TestID = self.edithear.GetValue()

            self.count += 1
            self.newStep='Continue.'
            (keepGoing, skip) = self.dialog.Update(self.count,self.newStep)
            myObject=myClass(TestID)
            print myObject.description

            self.count += 1
            self.newStep='Finished.'
            (keepGoing, skip) = self.dialog.Update(self.count,self.newStep)

            self.count = 0

            self.dialog.Destroy()

        except:
            self.dialog.Destroy()
            import sys, traceback
            xc = traceback.format_exception(*sys.exc_info())
            d = wx.MessageDialog( self, ''.join(xc),"Error",wx.OK)
            d.ShowModal() # Show it
            d.Destroy() #finally destroy it when finished

class myClass():
    def __init__(self,TestID):
        self.description = 'The variable name is:  '+str(TestID)+'. '

app = wx.PySimpleApp()
frame = MainWindow(None,-1,"My GUI")
app.MainLoop()
下面是setup.py的代码,该文件包含我的py2exe代码:

from distutils.core import setup
import py2exe

# Remove the build folder, a bit slower but ensures that build contains the latest
import shutil
shutil.rmtree("build", ignore_errors=True)

# my setup.py is based on one generated with gui2exe, so data_files is done a bit differently
data_files = []
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'pywin.debugger',
        'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
        'Tkconstants', 'Tkinter', 'pydoc', 'doctest', 'test', 'sqlite3'
        ]
packages = ['pytz']
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
            'tk84.dll']
icon_resources = []
bitmap_resources = []
other_resources = []

# add the mpl mpl-data folder and rc file
import matplotlib as mpl
data_files += mpl.get_py2exe_datafiles()

setup(
    windows=['GUIdiagnostics.py'],
                      # compressed and optimize reduce the size
    options = {"py2exe": {"compressed": 2, 
                      "optimize": 2,
                      "includes": includes,
                      "excludes": excludes,
                      "packages": packages,
                      "dll_excludes": dll_excludes,
                      # using 2 to reduce number of files in dist folder
                      # using 1 is not recommended as it often does not work
                      "bundle_files": 2,
                      "dist_dir": 'dist',
                      "xref": False,
                      "skip_archive": False,
                      "ascii": False,
                      "custom_boot_script": '',
                     }
          },

    # using zipfile to reduce number of files in dist
    zipfile = r'lib\library.zip',

    data_files=data_files
)
我通过在windows(cmd.exe)的命令行界面中按以下链接键入以下行来运行此代码:

setup.py py2exe
Py2exe随后运行,但当我尝试启动生成的exe文件时,它会创建一个日志文件,其中包含以下消息:

Traceback (most recent call last):
  File "setup.py", line 6, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "pylab.pyo", line 1, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\pylab.pyo", line 206, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\mpl.pyo", line 3, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\axes.pyo", line 14, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\collections.pyo", line 21, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\backend_bases.pyo", line 32, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\widgets.pyo", line 12, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "matplotlib\mlab.pyo", line 388, in <module>
TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict'
不幸的是,在命令行(cmd.exe)中使用以下命令运行它:

python cxsetup.py build
在命令行中生成以下错误:

ImportError: No module named cx_Freeze
命令行中的目录是我的应用程序的目录,它位于桌面的子文件夹中。这与python应用程序的目录不同,但我认为cmd.exe可以解决这个问题,因为python可以解决这个问题我错了吗?作为测试,我在cxsetup.py的第一行添加了以下代码:

import matplotlib
但这产生了几乎相同的错误:

ImportError: No module named matplotlib

我已经试着让这篇文章集中和简短,但它越来越长了有人能帮我吗?我不想做所有切换到cx_freeze的工作,却发现它无法与numpy、matplotlib、scipy等一起工作。

似乎是本文底部提到的问题:

看起来您需要对mlab.py进行一些小更改:

psd.__doc__ = psd.__doc__ % kwdocd


如果您还没有看到这个页面,我就是这样到达的:

正如其他人所提到的,py2exe似乎需要根据具体情况进行丑陋的随机修复。。。似乎没有办法解决这个问题。此外,一些错误拒绝消失,不会影响程序,但会导致程序通知用户退出后创建了错误日志。为了避免这种情况,我使用以下代码:

import sys
IS_USING_PY2EXE = hasattr(sys, "frozen")

# Redirect output to a file if this program is compiled.     
if IS_USING_PY2EXE:
    # Redirect log to a file.
    LOG_FILENAME = os.path.join(logDir, "myfile.log")
    print('Redirecting Stderr... to %s' % LOG_FILENAME)
    logFile = open(os.path.join(LOG_FILENAME),"w") # a --> append, "w" --> write

    sys.stderr = logFile
    sys.stdout = logFile

这可能只是我的愚蠢,但为什么不尝试将scipy从0.8.0b1更新到0.8.0,并对matplotlib执行相同的操作?Numpy 1.4.1应该还可以

也许您可以尝试使用cxfreeze,看看它是否有助于创建一个工作环境。我试过了,但是我从cxfreeze得到了一条不同的错误消息,我更愿意关注这个线程,这样我就不会因为太多的代码或太多的错误消息而混淆人们。编程的这一方面对我来说是新的。没问题,只是值得一试。我今天再次尝试了你的建议,得到了上面第二次编辑中描述的错误消息。我真的很想得到一些建议。我并不想让任何人因为切换到cx\ U冻结而感到困惑。我很高兴用py2exe做这件事。我只想要上面描述的与numpy、scipy、matplotlib等一起工作的东西。我有一种感觉,如果有人不提出一个简单的解决方案,这可能会变得毛茸茸的。我担心cx_freeze会像py2exe一样混乱。除非知道并花一点时间研究它的人能够对解决方案给出一个简单的总结。我所有的相关代码都在上面。
if psd.__doc__ is not None:
    psd.__doc__ = psd.__doc__ % kwdocd
else:
    psd.__doc__ = ""
import sys
IS_USING_PY2EXE = hasattr(sys, "frozen")

# Redirect output to a file if this program is compiled.     
if IS_USING_PY2EXE:
    # Redirect log to a file.
    LOG_FILENAME = os.path.join(logDir, "myfile.log")
    print('Redirecting Stderr... to %s' % LOG_FILENAME)
    logFile = open(os.path.join(LOG_FILENAME),"w") # a --> append, "w" --> write

    sys.stderr = logFile
    sys.stdout = logFile