16

I am currently making a game using pygame module. I have followed the following links' directions. https://pythonprogramming.net/converting-pygame-executable-cx_freeze/

I have solved some problems such as KeyError KeyError: 'TCL_Library' when I use cx_Freeze AttributeError Attribute Error while using cx_Freeze

The build of setup.py has been done, but as I run the exe file of the game, it crashes with Fatal Python error: Py_Initialize: unable to load the file system codec

    Fatal Python error: Py_Initialize: unable to load the file system codec
Traceback (most recent call last):
  File "C:\Users\jinju\AppData\Local\Programs\Python\Python35-32\lib\encodings\__init__.py", line 31, in <module>
ImportError: No module named 'codecs'

I have read the previous questions answered in Linux kernel (cx_Freeze: "No module named 'codecs'"), but I want to know what should I do with my Windows 10 kernel.

I use Python 3.5.3 downloaded through the python homepage, cx_Freeze-5.1-cp35-cp35m-win32.whl downloaded automatically through pip, pygame 1.9.3 through downloading whl with pip.

the setup.py file

import cx_Freeze, os

executables = [cx_Freeze.Executable("quatris.py")]

os.environ['TCL_LIBRARY'] = r'C:\Users\jinju\AppData\Local\Programs\Python\Python35-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\jinju\AppData\Local\Programs\Python\Python35-32\tcl\tk8.6'

cx_Freeze.setup(
    name = "QUATRIS",
    version = '1.0',
    options={"build_exe": {"packages":["pygame"], "include_files":['ab_main.wav', 'ct_main.wav', 'f3_main.wav',
                                                                    'ff_main.wav', 'gta4_soviet.wav',
                                                                    'h3_neverforget.wav', 'h_onefinaleffort.wav',
                                                                    'm_sweden.wav', 'p1_stillalive.wav',
                                                                    'p2_caramiaaddio.wav', 'smb_1-up.wav',
                                                                    'smb_bump.wav', 'smb_coin.wav', 'smb_gameover.wav',
                                                                    'smb_mariodie.wav', 'smb_overworld.wav',
                                                                    'smb_pause.wav', 'tes4o_main.wav',
                                                                    'tes5s_dragonborn.wav', 'tes5s_farhorizons.wav',
                                                                    'tetris.wav', 'tloz_intro.wav', 'tw3wh_main.wav']}},
    executables = executables)

I tried to look at the init.py file and the codecs.py file. When I try to import codecs,

module 'codecs' has no attribute 'register'

this thing comes out.

# Register the search_function in the Python codec registry
codecs.register(search_function)

This is the part where the error occurs in the init.py file.

Minha Armada Ju
  • 161
  • 1
  • 4
  • Your quatris.py runs without errors doesn't it? – Xantium Dec 15 '17 at 19:49
  • Even with `cx_freeze` 5.1.1 I was still getting this error. I followed the traceback all the way up and realized it was a particular module that was the source of the error. In my case it was `tweepy` module that was the source. I didn't need that module in the exe so I just commented it out and no longer got that error. – 5nizzard Nov 01 '18 at 15:59

2 Answers2

4

This was a bug in cx_Freeze which has now been corrected: https://github.com/anthony-tuininga/cx_Freeze/commit/9c98492911d4c75587d3687206d11812b48bf144

Anthony Tuininga
  • 6,388
  • 2
  • 14
  • 23
  • I just installed via `pip install cx_Freeze --upgrade` (v5.1) and am having the same problem. When I try to run the .exe file it crashes with the same error, `No module named 'codecs'`. Adding `import codec` to the top of my python file had no effect. I'm using Python 3.6.1 on windows with an amd64 cpu. – pheeper Dec 14 '17 at 21:02
  • Yes, you have to build from source until I release version 5.1.1! – Anthony Tuininga Dec 14 '17 at 22:35
  • 4
    cx_Freeze 5.1.1 was just released with this fix included in it. – Anthony Tuininga Dec 16 '17 at 18:17
  • I am having the same issue with cx_Freeze 5.1.1 – Amey P Naik Jul 25 '19 at 05:40
3

A simple approach but not elegant can be to place

if not True:
    import codecs

somewhere in your code.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25