从Win命令行运行py2exe构建的exe时缺少输出
我有一个脚本,叫做 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的命令行中运行 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的命令行中运行 my_script.exe
时,它的功能正常,但是 output_results
并没有像运行 python my_script.py
时那样在命令行中输出任何内容。
- 我该怎么解决这个问题,同时又只生成一个可执行文件呢?
- 我还没有尝试过pyinstaller或cx_freeze。这些工具处理这个问题会更好吗?
1 个回答
2
之前@Avaris在使用py2exe时打印不工作中解释过,windows
选项是在设置部分用来生成图形界面的可执行文件,这种文件是无法在控制台上打印输出的。正确的做法是使用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也有专门的流程来分别编译成图形界面和控制台的可执行文件。