cx_freeze:ModuleNotFoundError和程序停止工作

时间:2017-11-17 15:52:34

标签: python cx-freeze

我有一个控制台模式程序,我想用cx_Freeze分发。我刚刚在新安装的win 10机器上下载了python 3.6.3(amd64和win32版本)。我已经从轮子安装了cx_Freeze 5.1(而不是从pypi)。然后我已经构建了该程序的amd64和win32版本。如果我尝试在我构建它的同一台机器上执行该exe文件,我会收到此错误:

Fatal Python error: Py_Initialize: unable to load the file system codec
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\encodings\__init__.py", line 31, in <module>
ModuleNotFoundError: No module named 'codecs'

我在两种架构上都有同样的错误。安装文件如下所示:

#!/usr/bin/env python3
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
    "packages": [
        "os", "io", "copy", "struct", "hashlib", "random",
        "urllib", "pycurl", "json", "multiprocessing",
        "cryptography",
        "tornado", "watchdog", "pathtools"
    ],
    "includes": [
        "cryptography", "urllib.parse",
    ],
    "include_files": ["tmp", "server.ini", "client.ini"],
    "excludes": ["tkinter"],
}

setup(
    name="BlindBackup",
    version="1.0",
    description="BlindBackup",
    options={"build_exe": build_exe_options},
    executables=[
        Executable("backup.py", base=None),
        Executable("bsync.py", base=None),
        Executable("server.py", base=None),
    ]
)

换句话说,我已明确指定了“编解码器”包。

有什么问题?

UPDATE 分发目录中有一个名为library.zip的文件。它包含codecs.pyc但exe文件没有看到它。如果我从该zip中提取所有文件,那么我会收到不同的错误消息:

C:\Python\Projects\blindbackup\build\exe.win-amd64-3.6>backup.exe
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Program Files\Python36\lib\site-packages\cx_Freeze\initscripts\Console.py", line 22, in run
    importer = zipimport.zipimporter(os.path.dirname(os.__file__))
zipimport.ZipImportError: not a Zip file

我想问题是代码尝试从library.zip文件导入模块?但不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

解决方案是:

  • 安装cx_freeze 6,因为版本5不支持python 3.6(显然)
  • 安装pywin32(虽然我的程序不需要)
  • 将所有缺少的pyd文件添加到“includes”,将所有缺少的软件包添加到“packages”部分,将其添加到安装脚本中。