无法使用 virtualenv 和 cx_Freeze 将 pygame 转换为可执行文件

Posted

技术标签:

【中文标题】无法使用 virtualenv 和 cx_Freeze 将 pygame 转换为可执行文件【英文标题】:Unable to convert pygame to executable with virtualenv and cx_Freeze 【发布时间】:2018-01-12 19:59:24 【问题描述】:

我无法使用 cx_Freeze 将我根据 youtube 上的教程制作的游戏转换为可执行文件。我正在使用 virtualenv 并且 cx_Freeze 是 5.0 版。运行游戏时,我收到一条消息:

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

Current thread 0x00002c24 (most recent call first):

以下是创建文件时的回溯。

(pygame) G:\Programming\scripts\Python\PyGame\compile_files>python setup.py build
running build
running build_exe
creating directory build\exe.win32-3.5
copying G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.win32-3.5\Slither.exe
copying G:\Programming\scripts\Python\virtualenv\pygame\Scripts\python35.dll -> build\exe.win32-3.5\python35.dll
Traceback (most recent call last):
  File "setup.py", line 12, in <module>
    executables=executables
  File "G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
    distutils.core.setup(**attrs)
  File "G:\python\Python35-32\Lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "G:\python\Python35-32\Lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "G:\python\Python35-32\Lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "G:\python\Python35-32\Lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "G:\python\Python35-32\Lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "G:\python\Python35-32\Lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\dist.py", line 219, in run
    freezer.Freeze()
  File "G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\freezer.py", line 623, in Freeze
    self._FreezeExecutable(executable)
  File "G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\freezer.py", line 225, in _FreezeExecutable
    self._AddVersionResource(exe)
  File "G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\freezer.py", line 165, in _AddVersionResource
    trademarks = exe.trademarks)
  File "G:\Programming\scripts\Python\virtualenv\pygame\lib\site-packages\cx_Freeze\freezer.py", line 759, in __init__
    parts = version.split(".")
AttributeError: 'NoneType' object has no attribute 'split'

(pygame) G:\Programming\scripts\Python\PyGame\compile_files>`

我的设置文件是:

import cx_Freeze
import os
os.environ['TCL_LIBRARY'] = "G:\\python\\Python35-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "G:\\python\\Python35-32\\tcl\\tk8.6"

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

cx_Freeze.setup(
    name="Game",
    options="build_exe":"packages":["pygame"],"include_files":["image1.png","image2.png"],
    description="Game",
    executables=executables
)

图像与 setup.py 文件位于同一目录中。

【问题讨论】:

【参考方案1】:

在您的设置文件中使用它:

from cx_Freeze import setup,Executable
setup(name="NAME OF YOUR PROGRAM",
         version="1.0",
         description="as above",
         executables=[Executable("NAME OF THE SCRIPT.py")])

之后,将您的 png 文件拖到与您的程序相同的文件夹中。

python setup.py build

最后把你的 png,jpg 等文件放到那个文件夹里。

【讨论】:

我添加了我当前的 setup.py 文件,它看起来与您列出的相似。 还有其他想法吗?

以上是关于无法使用 virtualenv 和 cx_Freeze 将 pygame 转换为可执行文件的主要内容,如果未能解决你的问题,请参考以下文章

我无法使用 virtualenv 创建虚拟环境 [关闭]

Pycharm、virtualenv 和 kivy 设置

将 MySQLdb 安装到 virtualenv 后无法导入

virtualenv和pyenv是啥关系?

Macbook下virtualenv无法使用解决办法

python`deactivate`命令无法退出virtualenv