使用 PyInstaller 仅将 Python 字符串转换为浮点数失败

converting a Python string to float only fails with PyInstaller

我有一个运行良好的程序,但是当我通过 PyInstaller 将它变成可执行文件时,我遇到了问题。我将其追溯到证明问题的奇怪行为。在 main() 中,我放置了以下语句:

print float('1e-07')

当我 运行 我的正常 Python 脚本时,它按预期打印 '1e-07'

当我 运行 我的 PyInstaller .exe 程序时,它打印 'ERR'.

有人知道为什么会这样吗?

注意:这不是我创造的一些深奥的问题。我有一个相当大的应用程序,经常将字符串转换为浮点数。我发现读取以前创建的项目不再有效,我设法将其调试到这种效果。我应该注意,有时它会正确转换数字(在我的程序中),有时则不会。应用程序中的其他一切 运行 都很好 - 只是这个数字转换问题。人们用不同的方式来回答转换数字的问题,但效果并不好。任何答案都应该集中在为什么 Python 中如此简单的东西在使用 pyinstaller 创建应用程序时不起作用。目前,在解决此问题之前,我无法构建此软件的版本!

更新: 现在我创建了一个仅包含一行的程序,该程序打印此值并在 Pyinstaller 生成的代码中打印为 'ERR' - 一定是 PyInstaller 的问题,或者是我没有包含库或其他东西。任何帮助将不胜感激。

文件如下所示:

def main():  
    print float('1e-07')  

if __name__ == '__main__':  
    main()    

再次更新:

根据评论中的建议,我的代码现在如下所示:

import traceback
import sys

print "test"
try:
    print float("1e-07")
except:
    traceback.print_exc(file=sys.stdout)

没有异常被抛出,代码只是打印 'ERR' - 请注意,这仅在 PyInstaller 生成的 .exe 文件中 - 当 PyScripter 运行 它执行正常。

PyScripter 的输出:
测试
1e-07

PyInstaller 生成的 .exe 的输出:
测试
错误

命令行加上输出:

pyinstaller test.py  
156 INFO: PyInstaller: 3.2  
156 INFO: Python: 2.7.3  
156 INFO: Platform: Windows-7-6.1.7601-SP1  
156 INFO: wrote C:\work\PySI\PySIApp\test.spec  
156 INFO: UPX is not available.  
156 INFO: Extending PYTHONPATH with paths  
['C:\work\PySI\PySIApp', 'C:\work\PySI\PySIApp']
156 INFO: checking Analysis  
156 INFO: Building Analysis because out00-Analysis.toc is non existent  
156 INFO: Initializing module dependency graph...  
156 INFO: Initializing module graph hooks...  
218 INFO: running Analysis out00-Analysis.toc  
218 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
  required by c:\python27\python.exe  
328 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.2
1022.8_none_60a5df56e60dc5df.manifest  
328 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.1_none_8550c6b5d18a9128.manifest  
328 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.4148_none_f47e1bd6f6571810.manifest  
328 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.4940_none_f47ed0f6f6564d90.manifest  
328 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.6161_none_f480bfaef65491a5.manifest  
437 INFO: Searching for assembly x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_none ...  
437 INFO: Found manifest C:\Windows\WinSxS\Manifests\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.307
29.6161_none_50934f2ebcb7eb57.manifest  
437 INFO: Searching for file msvcr90.dll  
437 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_5
0934f2ebcb7eb57\msvcr90.dll  
437 INFO: Searching for file msvcp90.dll  
437 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_5
0934f2ebcb7eb57\msvcp90.dll  
437 INFO: Searching for file msvcm90.dll  
437 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_5
0934f2ebcb7eb57\msvcm90.dll  
546 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.2
1022.8_none_60a5df56e60dc5df.manifest  
546 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.1_none_8550c6b5d18a9128.manifest  
546 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.4148_none_f47e1bd6f6571810.manifest  
546 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.4940_none_f47ed0f6f6564d90.manifest  
546 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.3
0729.6161_none_f480bfaef65491a5.manifest  
546 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 6161)  
5677 INFO: Caching module hooks...  
5677 INFO: Analyzing C:\work\PySI\PySIApp\test.py  
8065 INFO: Loading module hooks...  
8065 INFO: Loading module hook "hook-httplib.py"...  
8081 INFO: Loading module hook "hook-encodings.py"...  
8532 INFO: Looking for ctypes DLLs  
8549 INFO: Analyzing run-time hooks ...  
8564 INFO: Looking for dynamic libraries  
17331 INFO: Looking for eggs  
17331 INFO: Using Python library c:\python27\python27.dll  
17331 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'x86', oldVersion=(9, 0, 21022, 8)
, newVersion=(9, 0, 30729, 6161), publicKeyToken=u'1fc8b3b9a1e18e3b')]  
17331 INFO: Warnings written to C:\work\PySI\PySIApp\build\test\warntest.txt  
17393 INFO: checking PYZ  
17393 INFO: Building PYZ because out00-PYZ.toc is non existent  
17393 INFO: Building PYZ (ZlibArchive) C:\work\PySI\PySIApp\build\test\out00-PYZ.pyz  
17846 INFO: checking PKG  
17846 INFO: Building PKG because out00-PKG.toc is non existent  
17846 INFO: Building PKG (CArchive) out00-PKG.pkg  
18017 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe  
18017 INFO: checking EXE  
18017 INFO: Building EXE because out00-EXE.toc is non existent  
18017 INFO: Building EXE from out00-EXE.toc  
18017 INFO: Appending archive to EXE C:\work\PySI\PySIApp\build\test\test.exe  
18032 INFO: checking COLLECT  
18032 INFO: Building COLLECT because out00-COLLECT.toc is non existent  
18032 INFO: Building COLLECT out00-COLLECT.toc  
18032 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 6161)  

这是 warntest.txt 中的内容:

missing module named unicodedata.ucd_3_2_0 - imported by unicodedata, stringprep, encodings.idna  
missing module named array.array - imported by array, httplib  
missing module named _warnings.warn_explicit - imported by _warnings, warnings  
missing module named _warnings.filters - imported by _warnings, warnings  
missing module named _warnings.once_registry - imported by _warnings, warnings  
missing module named _warnings.default_action - imported by _warnings, warnings  
missing module named _warnings.warn - imported by _warnings, warnings  
missing module named _subprocess.STD_OUTPUT_HANDLE - imported by _subprocess, subprocess  
missing module named _subprocess.CREATE_NEW_PROCESS_GROUP - imported by _subprocess, subprocess  
missing module named _subprocess.STD_INPUT_HANDLE - imported by _subprocess, subprocess  
missing module named _subprocess.STARTF_USESHOWWINDOW - imported by _subprocess, subprocess  
missing module named _subprocess.CREATE_NEW_CONSOLE - imported by _subprocess, subprocess  
missing module named _subprocess.STD_ERROR_HANDLE - imported by _subprocess, subprocess  
missing module named _subprocess.STARTF_USESTDHANDLES - imported by _subprocess, subprocess  
missing module named _subprocess.SW_HIDE - imported by _subprocess, subprocess
missing module named cPickle.loads - imported by cPickle, collections  
missing module named cPickle.dumps - imported by cPickle, collections  
missing module named thread.get_ident - imported by thread, collections  
missing module named thread._local - imported by thread, threading  
missing module named thread.stack_size - imported by thread, threading  
missing module named thread.allocate_lock - imported by thread, _strptime  
missing module named itertools.chain - imported by itertools, heapq, collections  
missing module named itertools.starmap - imported by itertools, collections  
missing module named itertools.repeat - imported by itertools, heapq, collections  
missing module named itertools.ifilterfalse - imported by itertools, sets  
missing module named itertools.ifilter - imported by itertools, sets  
missing module named itertools.imap - imported by itertools, heapq  
missing module named itertools.tee - imported by itertools, heapq  
missing module named itertools.izip - imported by itertools, heapq  
missing module named itertools.count - imported by itertools, heapq  
missing module named itertools.islice - imported by itertools, repr, heapq  
missing module named operator.itemgetter - imported by operator, collections, heapq  
missing module named operator.attrgetter - imported by operator, inspect  
missing module named _collections.deque - imported by _collections, collections  
missing module named _collections.defaultdict - imported by _collections, collections  
missing module named _weakref.ref - imported by _weakref, _weakrefset, weakref  
missing module named _weakref.ReferenceType - imported by _weakref, weakref  
missing module named _weakref.proxy - imported by _weakref, weakref  
missing module named _weakref.getweakrefs - imported by _weakref, weakref  
missing module named _weakref.getweakrefcount - imported by _weakref, weakref  
missing module named _weakref.ProxyType - imported by _weakref, weakref  
missing module named _weakref.CallableProxyType - imported by _weakref, weakref  
missing module named time.time - imported by time, threading  
missing module named time.sleep - imported by time, threading  
missing module named datetime.date - imported by datetime, _strptime  
missing module named errno.ENOENT - imported by errno, gettext  
missing module named _functools.partial - imported by _functools, functools  
missing module named _functools.reduce - imported by _functools, functools  
missing module named copy.deepcopy - imported by copy, weakref, sets  
missing module named exceptions.ReferenceError - imported by exceptions, weakref  
missing module named binascii.hexlify - imported by binascii, random  
missing module named math.sin - imported by math, random  
missing module named math.sqrt - imported by math, random  
missing module named math.cos - imported by math, random  
missing module named math.acos - imported by math, random  
missing module named math.ceil - imported by math, random  
missing module named math.exp - imported by math, random  
missing module named math.log - imported by math, random  
missing module named math.e - imported by math, random  
missing module named math.pi - imported by math, random  
missing module named _ctypes.call_function - imported by _ctypes, pyreadline.console.console  
missing module named _ctypes._wstring_at_addr - imported by _ctypes, ctypes  
missing module named _ctypes._string_at_addr - imported by _ctypes, ctypes  
missing module named _ctypes._cast_addr - imported by _ctypes, ctypes  
missing module named _ctypes._memset_addr - imported by _ctypes, ctypes  
missing module named _ctypes._memmove_addr - imported by _ctypes, ctypes  
missing module named _ctypes.get_last_error - imported by _ctypes, ctypes  
missing module named _ctypes.set_last_error - imported by _ctypes, ctypes  
missing module named _ctypes._check_HRESULT - imported by _ctypes, ctypes  
missing module named _ctypes.set_conversion_mode - imported by _ctypes, ctypes  
missing module named _ctypes._pointer_type_cache - imported by _ctypes, ctypes  
missing module named _ctypes.POINTER - imported by _ctypes, ctypes  
missing module named _ctypes._SimpleCData - imported by _ctypes, ctypes  
missing module named _ctypes.get_errno - imported by _ctypes, ctypes  
missing module named _ctypes.set_errno - imported by _ctypes, ctypes  
missing module named _ctypes.alignment - imported by _ctypes, ctypes  
missing module named _ctypes.addressof - imported by _ctypes, ctypes  
missing module named _ctypes.resize - imported by _ctypes, ctypes  
missing module named _ctypes.byref - imported by _ctypes, ctypes  
missing module named _ctypes.sizeof - imported by _ctypes, ctypes  
missing module named _ctypes.dlopen - imported by _ctypes, ctypes  
missing module named _ctypes.FUNCFLAG_STDCALL - imported by _ctypes, ctypes  
missing module named _ctypes.LoadLibrary - imported by _ctypes, ctypes  
missing module named _ctypes.FUNCFLAG_USE_ERRNO - imported by _ctypes, ctypes  
missing module named _ctypes.FUNCFLAG_USE_LASTERROR - imported by _ctypes, ctypes  
missing module named _ctypes.FUNCFLAG_PYTHONAPI - imported by _ctypes, ctypes  
missing module named _ctypes.FUNCFLAG_CDECL - imported by _ctypes, ctypes  
missing module named _ctypes.FormatError - imported by _ctypes, ctypes  
missing module named _ctypes.ArgumentError - imported by _ctypes, ctypes  
missing module named _ctypes.RTLD_LOCAL - imported by _ctypes, ctypes  
missing module named _ctypes.RTLD_GLOBAL - imported by _ctypes, ctypes  
missing module named _ctypes.__version__ - imported by _ctypes, ctypes  
missing module named _ctypes.CFuncPtr - imported by _ctypes, ctypes  
missing module named _ctypes._Pointer - imported by _ctypes, ctypes  
missing module named _ctypes.Structure - imported by _ctypes, ctypes  
missing module named _ctypes.Array - imported by _ctypes, ctypes  
missing module named _ctypes.Union - imported by _ctypes, ctypes  
missing module named struct.calcsize - imported by struct, ctypes  
missing module named pyreadline.console.ansi.AnsiWriter - imported by pyreadline.console.ansi, pyreadline.console.console  
missing module named pyreadline.console.ansi.AnsiState - imported by pyreadline.console.ansi, pyreadline.console.console, pyreadline.console.ironpython_console  
missing module named pyreadline.keysyms.KeyPress - imported by pyreadline.keysyms, pyreadline.console.console  
missing module named pyreadline.keysyms.make_KeyPress - imported by pyreadline.keysyms, pyreadline.console.ironpython_console, pyreadline.console.console  
missing module named pyreadline.keysyms.make_keyinfo - imported by pyreadline.keysyms, pyreadline.console.ironpython_console  
missing module named pyreadline.keysyms.make_KeyPress_from_keydescr - imported by pyreadline.keysyms, pyreadline.console.ironpython_console  
missing module named pyreadline.keysyms.make_keysym - imported by pyreadline.keysyms, pyreadline.console.ironpython_console  
missing module named startup - imported by pyreadline.keysyms.common, pyreadline.keysyms.keysyms  
missing module named System - imported by pyreadline.clipboard.ironpython_clipboard, pyreadline.keysyms.ironpython_keysyms, pyreadline.console.ironpython_console, pyreadline.rlmain  
missing module named _scproxy._get_proxy_settings - imported by _scproxy, urllib  `enter code here`
missing module named _scproxy._get_proxies - imported by _scproxy, urllib  
missing module named _scproxy - imported by urllib  
missing module named EasyDialogs.AskPassword - imported by EasyDialogs, getpass  
missing module named EasyDialogs - imported by getpass  
missing module named termios - imported by getpass  
missing module named pwd - imported by posixpath, getpass  
missing module named SOCKS - imported by ftplib  
missing module named binascii.b2a_qp - imported by binascii, quopri  
missing module named binascii.a2b_qp - imported by binascii, quopri  
missing module named socket.getnameinfo - imported by socket, ssl  
missing module named socket.error - imported by socket, ssl  
missing module named socket.socket - imported by socket, ssl  
missing module named socket._delegate_methods - imported by socket, ssl  
missing module named socket._fileobject - imported by socket, ssl  
missing module named _ssl.PROTOCOL_SSLv2 - imported by _ssl, ssl  
missing module named _ssl.PROTOCOL_TLSv1 - imported by _ssl, ssl  
missing module named _ssl.PROTOCOL_SSLv23 - imported by _ssl, ssl  
missing module named _ssl.PROTOCOL_SSLv3 - imported by _ssl, ssl  
missing module named _ssl.SSL_ERROR_EOF - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_WANT_WRITE - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_ZERO_RETURN - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_WANT_X509_LOOKUP - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_SSL - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_WANT_CONNECT - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_INVALID_ERROR_CODE - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_WANT_READ - imported by _ssl, ssl, socket  
missing module named _ssl.SSL_ERROR_SYSCALL - imported by _ssl, ssl, socket  
missing module named _ssl.RAND_status - imported by _ssl, ssl, socket  
missing module named _ssl.RAND_egd - imported by _ssl, ssl, socket  
missing module named _ssl.RAND_add - imported by _ssl, ssl, socket  
missing module named _ssl.CERT_REQUIRED - imported by _ssl, ssl  
missing module named _ssl.CERT_NONE - imported by _ssl, ssl  
missing module named _ssl.CERT_OPTIONAL - imported by _ssl, ssl  
missing module named _ssl.SSLError - imported by _ssl, ssl, socket  
missing module named _ssl.OPENSSL_VERSION_NUMBER - imported by _ssl, ssl  
missing module named _ssl.OPENSSL_VERSION_INFO - imported by _ssl, ssl  
missing module named _ssl.OPENSSL_VERSION - imported by _ssl, ssl  
missing module named rourl2path.url2pathname - imported by rourl2path, urllib  
missing module named rourl2path.pathname2url - imported by rourl2path, urllib  
missing module named rourl2path - imported by urllib  
missing module named sys.py3kwarning - imported by sys, httplib  
missing module named binascii.b2a_base64 - imported by binascii, email.base64mime  
missing module named binascii.a2b_base64 - imported by binascii, email.base64mime  
missing module named sys.stderr - imported by sys, smtplib  
missing module named IronPythonConsole - imported by pyreadline.console.ironpython_console  
missing module named clr - imported by pyreadline.clipboard.ironpython_clipboard, pyreadline.console.ironpython_console  
missing module named errno.EINVAL - imported by errno, StringIO  
missing module named cStringIO.StringIO - imported by cStringIO, gettext, pickle, pprint, socket, email.parser, email.message, tempfile, quopri, httplib, urllib, email.generator, email.iterators, shlex, encodings.uu_codec, encodings.quopri_codec  
missing module named binascii.a2b_uu - imported by binascii, encodings.uu_codec  
missing module named binascii.b2a_uu - imported by binascii, encodings.uu_codec  
missing module named 'org.python'.PyStringMap - imported by 'org.python', pickle  
missing module named 'org.python' - imported by pickle  
missing module named _struct.__doc__ - imported by _struct, struct  
missing module named _struct._clearcache - imported by _struct, struct  
missing module named fcntl - imported by subprocess, tempfile  
missing module named threading.RLock - imported by threading, _threading_local  
missing module named threading.current_thread - imported by threading, _threading_local  
missing module named traceback.format_exc - imported by traceback, threading  
missing module named os.urandom - imported by os, random  
missing module named riscosenviron._Environ - imported by riscosenviron, os  
missing module named riscosenviron - imported by os  
missing module named riscospath - imported by os  
missing module named riscos._exit - imported by riscos, os  
missing module named riscos - imported by os  
missing module named ce._exit - imported by ce, os  
missing module named ce - imported by os  
missing module named _emx_link.link - imported by _emx_link, os  
missing module named _emx_link - imported by os  
missing module named os2._exit - imported by os2, os  
missing module named os2 - imported by os  
missing module named nt._isdir - imported by nt, ntpath  
missing module named nt._getfullpathname - imported by nt, ntpath  
missing module named nt._exit - imported by nt, os  
missing module named posix._exit - imported by posix, os  
missing module named posix - imported by os  
missing module named resource - imported by posix  
missing module named strop.whitespace - imported by strop, string  
missing module named strop.maketrans - imported by strop, string  
missing module named strop.lowercase - imported by strop, string  
missing module named strop.uppercase - imported by strop, string  
missing module named org.PyStringMap - imported by org, copy  
missing module named org - imported by copy  
missing module named re._compile - imported by re, sre  
missing module named re.__all__ - imported by re, sre  
missing module named codecs.mbcs_encode - imported by codecs, encodings.mbcs  
missing module named codecs.mbcs_decode - imported by codecs, encodings.mbcs  

虽然我无法回答为什么会这样,但 eval('1e-07') 解决了这个问题。

以下代码工作正常。

eval('1e-07')
print type(eval('1e-07'))

以上代码计算为浮点数。

哇!折腾了一早上,问题终于找到了。

这被追踪到一个 dll - python27.dll。

我在朋友的机器上构建了应用程序(他来自 2.7.10)并在我的 pyinstaller 构建之上复制了这个 dll,从而替换了我的 dll(来自 2.7.3)现在一切正常。

我曾尝试复制许多其他可疑的 .dll,但复制这个并且只有这个 dll 解决了问题。

我随后将我的整个 python 安装更新到 2.7.11 - 最新版本并构建了测试程序,最后它根据需要打印出 1e-07。

然后,在解决了我通过更新所有这些(包括 pyinstaller)产生的所有新问题之后,我能够构建我的发布应用程序。

令人震惊和惊讶的是,显然 Python 2.7.3 在 PyInstaller 生成的独立可执行文件中存在这个问题,但事实似乎确实如此。