使用py2exe构建exe时,输出缺失是从Win CLI运行的

时间:2014-05-14 16:01:50

标签: python python-2.7 py2exe

我有一个包含这些功能的脚本my_script.py

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)

当我从Windows CLI运行python my_script.py时,output_results会按预期打印出以下内容。

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.

然后我使用py2exe和此安装文件将my_script.py构建到my_script.exe

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,
)

当我从Windows CLI运行my_script.exe时,它正常运行除了,因为output_results没有向命令行输出任何内容,就像我运行{{ 1}}。

  • 如何修复此问题并且仍然只创建了一个可执行文件?
  • 我没有尝试过pyinstaller或cx_freeze。这些处理选项能更好地处理这个吗?

1 个答案:

答案 0 :(得分:2)

正如@Avaris在Print not working when compiled with py2exe中所解释的那样 - 设置部分中的windows选项旨在构建GUI可执行文件,这些可执行文件不处理打印到控制台。正确的选择是使用console部分。

所以,而不是

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

使用

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

我不能对cx_freeze说,但是pyinstaller也有单独的例程,可以编译成gui和console可执行文件。