3

我正在使用 Python 3.2 和 Pygame 制作游戏。我已经成功地使用cx_freeze将所有内容捆绑到一个可执行文件中,并且它运行了。美好的。唯一的问题是,即使我将-OO标志传递给 my setup.py,我的游戏也是在调试模式下编译的。 (我已经用确实是的print陈述证实了这一点。)__debug__True

问题是,我的游戏具有在发布模式下自动禁用的调试功能。我不想分发我的游戏的调试功能,也不想手动从代码中删除它们。

为简洁起见,此处缩写为My setup.py,如下所示:

from cx_Freeze import setup, Executable

includes     = [<some modules>]
excludes     = [<some unwanted modules>]
include_files = [<game assets>]


build_options = {
                 'append_script_to_exe':True,
                 'bin_excludes':excludes,
                 'compressed':True,
                 'excludes': excludes,
                 'include_files': include_files,
                 'includes': includes,
                 'optimize':2,
                 'packages': ['core', 'game'],
                 }

common_exe_options = {
                      'appendScriptToExe'  : True,
                      'appendScriptToLibrary':True,
                      'compress'           : True,
                      'copyDependentFiles' : True,
                      'excludes'           : excludes,
                      'includes'           : includes, 
                      'script'             : '__init__.py',
                     }

executable = Executable(**common_exe_options)

setup(name='Invasodado',
      version='0.8',
      description='wowza!',
      options = {'build_exe': build_options,
                 'bdist_msi': build_options},
      executables=[executable])

与我的其他代码一样,完整的脚本可以在https://github.com/CorundumGames/Invasodado/blob/master/setup.py找到。

在 Ubuntu 12.10 上,我正在使用python3.2 -OO setup.py build. 在 Windows XP 上,我正在使用C:\Python32\python -OO setup.py build.

任何帮助都是极好的!

4

1 回答 1

2

有两件稍微不同的事情:将代码编译为字节码的优化,以及解释器运行时的优化。设置optimizecx_Freeze 的选项会优化它生成的字节码,但解释器仍然使用__debug__ == True.

似乎没有简单的方法为嵌入式解释器设置调试标志。它忽略了PYTHONOPTIMIZE环境变量。作为一种解决方法,您可以使用如下调试标志:

debug = __debug__ and not hasattr(sys, 'frozen')
于 2013-03-11T00:45:22.220 回答