Search code examples
pythoncx-freeze

cx_freeze: ModuleNotFoundError and program stopped working


I have a console mode program that I would like to distribute with cx_Freeze. I have just downloaded python 3.6.3 (both amd64 and win32 version) on a newly installed win 10 machine. I have installed cx_Freeze 5.1 from wheels (not from pypi). Then I have built both amd64 and win32 versions of the program. If I try to execute that exe file on the same machine where I have built it, I get this error:

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'

I have the same error on both architectures. The setup file looks like this:

#!/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),
    ]
)

In other words, I have specified the "codecs" package explicitly.

What is wrong?

UPDATE There is a file called library.zip in the distribution dir. It contains codecs.pyc but the exe file does not see it. If I extract all files from that zip, then I get a different error message:

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

I guess that the problem is with the code that tries to import modules from the library.zip file? But not sure how to fix it.


Solution

  • The solution was this:

    • install cx_freeze 6, because version 5 does not support python 3.6 (apparently)
    • install pywin32 (although it was not required for my program)
    • add all missing pyd files to "includes" and all missing packages to "packages" section it the setup script.