【问题标题】:basic pyinstaller on MacOS XMacOS X 上的基本 pyinstaller
【发布时间】:2013-02-25 20:37:22
【问题描述】:

我正在尝试让 pyinstaller 使用我的失败的 python 脚本。所以我尝试了一个非常基本的脚本:

#!/usr/bin/env python
from matplotlib.pyplot import *
from numpy import *
x=linspace(0,2*pi,200)
plot(x,sin(x))
show()

但这也会失败,并显示以下错误消息。我在最新的 Mountain Lion 上,如果重要的话,我正在使用 enthought python。我在 pyinstaller 目录中时用python pyinstaller.py --onefile ../testpyinst.py 调用它,完整的输出在这里:

23 INFO: wrote xxxxxxxxxxxxxx/pyinstaller-2.0/testpyinst/testpyinst.spec
54 INFO: UPX is not available.
1263 INFO: checking Analysis
1337 INFO: checking PYZ
1350 INFO: checking PKG
1350 INFO: building because out00-PKG.toc missing or bad
1350 INFO: building PKG out00-PKG.pkg
Traceback (most recent call last):
  File "pyinstaller.py", line 91, in <module>
    main()
  File "pyinstaller.py", line 86, in main
    run_build(opts, spec_file)
  File "pyinstaller.py", line 50, in run_build
    PyInstaller.build.main(spec_file, **opts.__dict__)
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/build.py", line 1625, in main
    build(specfile, buildpath)
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/build.py", line 1582, in build
    execfile(spec)
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/testpyinst/testpyinst.spec", line 16, in <module>
    console=True )
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/build.py", line 987, in __init__
    crypt=self.crypt)
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/build.py", line 880, in __init__
    self.__postinit__()
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/build.py", line 315, in __postinit__
    self.assemble()
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/build.py", line 933, in assemble
    archive.build(self.name, mytoc)
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/loader/archive.py", line 202, in build
    self.save_toc(tocpos)
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/loader/carchive.py", line 250, in save_toc
    tocstr = self.toc.tobinary()
  File "xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/loader/carchive.py", line 79, in tobinary
    nmlen+entrylen, dpos, dlen, ulen, flag, typcd, nm+pad))
struct.error: argument for 's' must be a string

【问题讨论】:

  • 您的代码看起来不错,您可能需要发布更多回溯,以便人们可以看到错误开始的位置(该错误不是来自您编写和发布的脚本)。
  • 谢谢,我现在添加了完整的输出。

标签: python macos pyinstaller


【解决方案1】:

您是否使用pyinstaller 成功构建了任何东西?我建议你写一个没有外部依赖的更简单的代码,

print "Hello World!"

f = open('test.txt','w')
f.write("Hello World!")
f.close()

尝试导入标准库模块,例如math

import math
x = 10.0
y = math.sqrt(x)
print "square_root({}) = {}".format(x,y)

接下来尝试使用numpy,它只打印sin(x),而不是尝试绘制它。

from numpy import *
x = linspace(0,2*pi,20)
print sin(x)

如果这有效,也许不是showing 绘图,而是尝试savefig 并查看错误是否与尝试显示图形有关。

from matplotlib.pyplot import *
from numpy import *
x=linspace(0,2*pi,200)
plot(x,sin(x))
savefig("/tmp/testfig.png")

如果这仍然不起作用,则可能是您的 matplotlib 后端有问题。使用更简单/更标准的:

import matplotlib
matplotlib.use("Agg")
from matplotlib.pyplot import *
from numpy import *
x=linspace(0,2*pi,200)
plot(x,sin(x))
savefig("/tmp/testfig.png")

【讨论】:

  • 不,即使使用最简单的第一个选项print sin(x),它也不起作用并提供与以前相同的回溯。
  • 我又加了一步:看看能不能导入标准库模块。可能你的问题出在 numpy 的安装上。
  • 好的,问题的一部分是选项--onefile。一旦我删除了它,我就可以完成上述大部分工作。为了绘制一些东西,我需要添加选项--windowed,它应该可以正常工作,但是对于与show()matplotlib.use("WxAgg") 的交互使用,我确实得到了窗口,但是当我的鼠标进入图窗。错误信息是MKL FATAL ERROR: Cannot load libmkl_core.dylib
  • 我相信我们现在已经超越了我的专业知识,抱歉,我无法提供更多帮助。现在您已经运行它并且完全有一个不同的错误,也许您有足够的信息可以在其他地方发布更详细的问题(也许这些人:groups.google.com/forum/?fromgroups=#!forum/pyinstaller
  • @JohnSmith 你有没有解决这个问题?我在使用 matplotlib 和 PyInstaller 时遇到了类似的问题,但我一直无法弄清楚。
【解决方案2】:

我所做的是打开这个文件xxxxxxxxxxxxxx/pyinstaller-2.0/PyInstaller/loader/carchive.py,并在第79行之后添加几个print()。我发现nm+pad没有被识别为字符串。不过这有点奇怪。我正在使用 Windows 7,print() 显示 nm=kernel32pad = ''

现在谈谈我的临时解决方案:

rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's', nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm + pad))

----改成----->

try:
    rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's', nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm + pad))
except:
    ss = str(nm + pad)
    rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's', nmlen + entrylen, dpos, dlen, ulen, flag, typcd, ss))

不确定它是否是一个强大的解决方案,但它适用于我的解决方案。我相信你可以使用类似的技术来找出你的。顺便说一句,我正在使用 pyinstaller2.1 + python2.7.6。命令是 pyinstaller -F MyApp.py --hidden-import=scipy.special._ufuncs_cxx

【讨论】:

  • 澄清一下:这里更改的代码行解决了当您将鼠标悬停在图表上时可执行文件崩溃的问题?我能够找到这条线,但不幸的是,改变它对我没有影响。 hidden-import 标志是否与此问题相关?
  • 从来没有鼠标悬停的问题。以上我正在尝试解决原始帖子中“s”的结构错误。
【解决方案3】:

我的问题与 hxu 的评论中描述的相同;改变

rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's', nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm + pad))

rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's', nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm.encode('utf8') + pad))

对我有用,因为 type(nm) 是 'unicode',而 type(pad) str.

【讨论】:

    【解决方案4】:

    我在使用 Win7 和 Pythonxy 时遇到了类似的问题。 我必须编辑文件 C:\Python27\Lib\site-packages\pyinstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_carchive.py 第 84-85 行,并将 nm + pad 更改为 str(nm + pad)

    原文:

    rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's',nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm + pad))
    

    修复:

    rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + 's',nmlen + entrylen, dpos, dlen, ulen, flag, typcd, str(nm + pad)))
    

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2017-06-16
      • 2018-12-15
      • 2012-08-06
      • 2014-01-12
      相关资源
      最近更新 更多