使用cx_freeze编译为exe后,Python 3.3 argparse错误

时间:2014-05-19 14:20:29

标签: python-3.3 cx-freeze

尝试编译Python 3.3应用程序,但在使用Windows 7上的cx_freeze将其编译为win32 exe后遇到问题。

示例:

Test.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import tkinter.messagebox


cl_parser = argparse.ArgumentParser(
    description='Test app Command Line Parameters')
cl_parser.add_argument('--version', action='version', version='%(prog)s 1.0')
cl_parser.add_argument('-s', '--show', help='show simple message box',
                       action='store_true')

cl_args = cl_parser.parse_args()

if cl_args.show:
    tkinter.messagebox.showinfo(title='Simple Message', message='Hi There')

Cx_Freeze脚本

import sys
from cx_Freeze import setup, Executable

import shutil

#Remove Build/Dist Folder Before Compile
args = None
print('Removing Old Folders...')
try:
    for args in sys.argv:
        if args.find('build_exe') != -1:
            shutil.rmtree('build', ignore_errors=True)
        elif args.find('bdist_msi') != -1:
            shutil.rmtree('dist', ignore_errors=True)
except:
    pass
finally:
    del args
    print('Folders Removed')

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

options = {
    'build_exe': {
        'includes': 'atexit',
        'include_msvcr': True
    }
}

executables = [
    Executable('Test.py', base=base, targetName='Test App.exe')
]

setup(name='Test App',
      version='1.0',
      description='Test App',
      options=options,
      executables=executables)

成功编译后,我在exe上尝试了几个命令行测试:

Test App.exe -s (This successfully shows the messagebox)
Test App.exe -h or TestApp.exe --version (Fails with AttributeError: 'NoneType' Object has no attribute 'write')

有没有办法可以让它变得有效?我正在尝试创建一个PyQt4应用程序,并且不希望将其编译为控制台应用程序。

您的意见将不胜感激

P.S。是的我知道我已经在tkinter中包含了一个示例,但这仅用于演示目的。

0 个答案:

没有答案