Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a Python string to float only fails with PyInstaller

I have a program that works fine, but when I make it into an executable via PyInstaller, I have problems. I traced this down to a weird behavior that demonstrates the problem. In main(), I place the following statement:

print float('1e-07')

When I run my normal Python script, it prints '1e-07' as expected.

When I run my PyInstaller .exe program, it prints 'ERR'.

Does anyone know why this is happening?

NOTE: This is not some esoteric problem I created. I have a rather large application that frequently converts strings to floats. I found that reading in previously created projects doesn't work anymore and I managed to debug this down to this one effect. I should note that sometimes it converts the numbers properly (in my program) and sometimes not. Everything else runs fine in the application - it's just this number conversion problem. People have answered with different ways of converting numbers that don't work any better. Any answer should concentrate on why something so simple in Python does not work when the application is created with pyinstaller. Currently, I cannot build a release of this software until I can fix this problem!

UPDATE: Now I have created a program consisting only of one line that prints this value and it prints as 'ERR' in the Pyinstaller generated code - must be a PyInstaller problem or a failure of mine to include a library or something. Any help would be appreciated.

The file looks like this:

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

if __name__ == '__main__':  
    main()    

Update Again:

Based on recommendations in comments, my code now looks like this:

import traceback
import sys

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

No exception is thrown and the code just prints 'ERR' - mind you this is only in the PyInstaller generated .exe file - when run from PyScripter it executes fine.

Output from PyScripter:
test
1e-07

Output from PyInstaller generated .exe:
test
ERR

Command Line plus output:

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)  

Here's what's in 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  
like image 766
Pete P Avatar asked May 12 '16 15:05

Pete P


People also ask

Why does PyInstaller not work?

The most common reason a PyInstaller package fails is that PyInstaller failed to bundle a required file. Such missing files fall into a few categories: Hidden or missing imports: Sometimes PyInstaller can't detect the import of a package or library, typically because it is imported dynamically.

What is hidden import in PyInstaller?

--hidden-import. List multiple top-level imports that PyInstaller was unable to detect automatically. This is one way to work around your code using import inside functions and __import__() . You can also use --hidden-import multiple times in the same command.

Does PyInstaller include imports?

Analysis: Finding the Files Your Program Needs To find out, PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.

What is PyInstaller used for?

PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter!


1 Answers

Whew! After all morning spent on this, the problem was found.

This was traced to one dll - python27.dll.

I built the application on a friend's machine (his was from 2.7.10) and literally copied this dll on top of my pyinstaller build, thus replacing my dll (from 2.7.3) and now everything worked.

I had tried copying many other suspicious .dlls, but copying this and only this dll fixed the problem.

I subsequently updated my entire python installation to 2.7.11 - the latest and built the test program and finally it printed out 1e-07 as desired.

Then, after dealing with all of the new problems I created by updating all of this (including pyinstaller), I was able to build my release application.

It is shocking and surprising that apparently Python 2.7.3 has this problem with PyInstaller generated stand-alone executables, but that seems to be the case.

like image 178
Pete P Avatar answered Sep 19 '22 06:09

Pete P