cx_Freeze:“没有名为'编解码器的模块'”Windows 10

时间:2017-11-10 13:30:49

标签: python python-3.x pygame cx-freeze

我目前正在使用pygame模块制作游戏。 我已按照以下链接'方向。 https://pythonprogramming.net/converting-pygame-executable-cx_freeze/

我已经解决了一些问题,例如KeyError KeyError: 'TCL_Library' when I use cx_Freeze AttributeError的 Attribute Error while using cx_Freeze

setup.py的构建已经完成,但是当我运行游戏的exe文件时,它崩溃了Fatal Python错误:Py_Initialize:无法加载文件系统编解码器

    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'

我已阅读过Linux内核(cx_Freeze: "No module named 'codecs'")中回答的问题,但我想知道如何处理Windows 10内核。

我使用通过python主页下载的Python 3.5.3,cx_Freeze-5.1-cp35-cp35m-win32.whl通过pip,pygame 1.9.3自动下载,通过pip下载whl。

setup.py文件

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)

我试着查看 init .py文件和codecs.py文件。当我尝试导入编解码器时,

module 'codecs' has no attribute 'register'

这件事出来了。

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

这是 init .py文件中出现错误的部分。

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:3)

一个简单的方法,但不优雅可以放置

if not True:
    import codecs

代码中的某处。