Skip to main content
 首页 » 编程设计

Python 3.2 : cx_freeze compiles my program, 但处于 Debug模式

2025年05月04日10me-sa

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

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

我的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 进行构建。

任何帮助都会很棒!

请您参考如下方法:

有两个稍微不同的事情:将代码编译为字节码的优化,以及解释器运行时的优化。在 cx_Freeze 的选项中设置 optimize 优化它生成的字节码,但解释器仍然以 __debug__ == True 运行。

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

debug = __debug__ and not hasattr(sys, 'frozen')