JesseTG
JesseTG

Reputation: 2123

Python 3.2: cx_freeze compiles my program, but in debug mode

I'm making a game with Python 3.2 and Pygame. I've successfully managed to use cx_freeze to bundle everything into an executable, and it runs. Fine. The only problem is, even when I pass the -OO flag to my setup.py, my game is compiled in debug mode. (I've confirmed it with print statements that __debug__ is indeed True.)

The problem is, my game has debugging features that are disabled automatically in release mode. I don't want to distribute the debugging features of my game, and I don't want to have to remove them from the code manually.

My setup.py, shortened here for brevity, is as follows:

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])

The full script, as with the rest of my code, can be found at https://github.com/CorundumGames/Invasodado/blob/master/setup.py .

On Ubuntu 12.10, I'm building with python3.2 -OO setup.py build. On Windows XP, I'm building with C:\Python32\python -OO setup.py build.

Any help would be awesome!

Upvotes: 3

Views: 1698

Answers (1)

Thomas K
Thomas K

Reputation: 40340

There are two slightly separate things: the optimisation for compiling your code to bytecode, and the optimisation the interpreter runs with. Setting optimize in the options for cx_Freeze optimises the bytecode it produces, but the interpreter still runs with __debug__ == True.

It seems there's no easy way to set the debug flag for the embedded interpreter. It ignores the PYTHONOPTIMIZE environment variable. As a workaround, you could do use a debug flag like:

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

Upvotes: 2

Related Questions