2

我正在尝试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
4

4 回答 4

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)

如果这样可行,也许不是show绘制情节,而是尝试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")
于 2013-02-25T21:25:57.080 回答
1

我所做的是打开这个文件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

于 2014-11-05T22:50:57.267 回答
0

我的问题与 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.

于 2014-12-05T09:55:51.257 回答
0

我在 Win7 和 Pythonxy 上遇到了类似的问题。我不得不编辑文件C:\Python27\Lib\site-packages\pyinstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_carchive.py第 84-85 行,并更改 nm + padstr(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)))
于 2015-05-28T21:20:37.303 回答