我有一个包含这些功能的脚本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}}。
答案 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可执行文件。