Domanda

I have a script, my_script.py that includes these functions.

def output_results(scrub_count, insert_count):
    print "Scrubbing Complete" 
    print "Valid requests: "+ str(scrub_count["Success"])
    if scrub_count["Error"] > 0:
        print "Requests with errors can be found in " + error_log
    print "\n"
    print "Inserting Complete"    
    print str(insert_count["Success"]) + " rows inserted into " + table + "."
    print str(insert_count["Error"]) + " rows were NOT inserted. Please see " + error_log + " for more details."

def main():
    scrub_count={"Success":0,"Error":0}
    insert_count={"Success":0,"Error":0}

    someOtherFunctions()

    output_results(scrub_count, insert_count)

When I run the python my_script.py from the Windows CLI, output_results prints out the following as expected.

Scrubbing Complete
Valid requests: 7
Invalid requests: 3
Requests with errors can be found in error.log

Inserting Complete
5 rows inserted into Table.
2 rows were NOT inserted. Please see error.log for more details.

I then build my_script.py into my_script.exe using py2exe and this setup file:

from distutils.core import setup
import py2exe, sys

sys.argv.append('py2exe')

setup(
    options = {'py2exe':{'bundle_files':1, 'compressed':True,'includes':["socket","decimal","uuid"]}},
    windows = [{'script':"C:\Path\To\my_script.py"}],
    zipfile = None,
)

When I run my_script.exe from the Windows CLI, it functions properly except for the fact that output_results does not output anything to the command line like when I run python my_script.py.

  • How can I fix this and still only have a single executable created?
  • I haven't tried pyinstaller or cx_freeze. Do these handle options handle this better?
È stato utile?

Soluzione

As previously explained by @Avaris in Print not working when compiled with py2exe - windows option in setup section is intended to build GUI executables, which do not handle printing to console. The proper option is to use console section.

So, instead of

setup(
    options = {'py2exe':{'bundle_files':1, 'compressed':True,'includes':["socket","decimal","uuid"]}},
    windows = [{'script':"C:\Path\To\my_script.py"}],
    zipfile = None,
)

use

setup(
    options = {'py2exe':{'bundle_files':1, 'compressed':True,'includes':["socket","decimal","uuid"]}},
    console = [{'script': "C:\Path\To\my_script.py"}],
    zipfile = None,
)

I can't say for cx_freeze, but pyinstaller also has separate routines for compiling into gui and console executables.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top