首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >py2exe和numpy相处不好

py2exe和numpy相处不好
EN

Stack Overflow用户
提问于 2011-02-04 06:20:32
回答 3查看 6K关注 0票数 7

我正在尝试使用py2ex-0.6.9.win 32来结束我用Python2.6.5编写的应用程序,它使用具有相关下载文件名的以下对象库:

matplotlib-0.99.3.win32

numpy-1.4.1-win32

scipy-0.8.0b1-win32

wxPython2.8-win32-unicode-2.8.11.0

当我试图启动生成的.exe文件时,我会收到错误消息。目前,错误消息与numpy有关,尽管在此之前,我得到了一些与matplot数据文件相关的内容,这些文件没有加载,从而阻止了我的exe文件的启动。

与其发布一英里长的代码和所有错误消息,我还会发布一个更普遍的问题:可以向我展示一些说明,使所有这些对象库和版本一起使用py2exe来创建一个工作的exe文件吗?

我一直在阅读谷歌搜索这一主题的东西,但这似乎是白费力气,因为每个人都在使用不同版本的不同的东西。我可以更改其中一些对象库的版本,但我已经在这个信号处理应用程序中编写了5000行代码,如果可能的话,我希望不必重写所有这些代码。

编辑:

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

代码语言:javascript
运行
复制
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代码的文件:

代码语言:javascript
运行
复制
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)的命令行接口中键入以下行来运行这段代码:

代码语言:javascript
运行
复制
setup.py py2exe

然后运行Py2exe,但是当我试图启动结果的exe文件时,它会创建一个包含以下消息的日志文件:

代码语言:javascript
运行
复制
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'

任何人都可以教我如何编辑setup.py,以便py2exe可以创建一个运行numpy、scipy、matplotlib等的可用的可执行文件?

第二次编辑:

好的。我今天再次尝试了RC的建议,现在我有了一个新的想法,我得到了同样的错误,但我包括在下面。下面是一个名为cxsetup.py的文件的代码,该文件是我在:Freeze.html上创建的模板之后创建的。

代码语言:javascript
运行
复制
from cx_Freeze import setup, Executable

setup(
        name = "Potark",
        version = "0.1",
        description = "My application.",
        executables = [Executable("Potark-ICG.py")])

不幸的是,在命令行( cmd.exe )中使用以下命令运行它:

代码语言:javascript
运行
复制
python cxsetup.py build

在命令行中生成以下错误:

代码语言:javascript
运行
复制
ImportError: No module named cx_Freeze

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

代码语言:javascript
运行
复制
import matplotlib

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

代码语言:javascript
运行
复制
ImportError: No module named matplotlib

我一直试图保持这条线的重点和短小,但它正在变得有点长。有人能帮我吗?,我不想做所有的切换到cx_freeze的工作,结果发现它不能与numpy、matplotlib、Can等一起工作。

EN

回答 3

Stack Overflow用户

发布于 2011-02-07 06:27:54

就像下面提到的问题:http://www.py2exe.org/index.cgi/MatPlotLib

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

代码语言:javascript
运行
复制
psd.__doc__ = psd.__doc__ % kwdocd

代码语言:javascript
运行
复制
if psd.__doc__ is not None:
    psd.__doc__ = psd.__doc__ % kwdocd
else:
    psd.__doc__ = ""

如果你还没看过这一页,我就是这样到达的:http://www.py2exe.org/index.cgi/WorkingWithVariousPackagesAndModules

票数 2
EN

Stack Overflow用户

发布于 2011-02-10 15:30:03

正如其他人所提到的,py2exe似乎需要在逐个案例的基础上进行丑陋的随机修复.似乎没有办法绕过它。此外,一些错误拒绝消失,不会影响程序,但确实会导致程序通知用户退出后创建了错误日志。为了避免这种情况,我使用以下代码:

代码语言:javascript
运行
复制
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
票数 0
EN

Stack Overflow用户

发布于 2011-03-18 21:37:36

这可能只是我的愚蠢,但为什么不尝试将您的with从0.8.0b1更新到0.8.0,并对matplotlib做同样的操作呢?Numpy 1.4.1应该还不错。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4895153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档