SoFunction
Updated on 2024-11-17

Python cx_freeze packaging tool handling problem ideas and solutions

Here are the problems and solutions encountered in using cx_freeze (Win7)

1. Description of the problem:Running an exe that launches countless main programs makes the system unusable

  Reason: using multiprocessing package in the program

  Workaround: add multiprocessing.freeze_support() after if __name__ == "__main__": in the main file, be sure to add it at the very beginning of the

2. Description of the problem:After running it, I get an exception in freeze_support.

 Reason: PyQT is used as the interface and there is no console.

 Workaround: redirect stdout and stderr before calling multiprocessing.freeze_support(), add: = () and = ()

3. Using the shelve moduleThe debugging under IDE works fine, but it doesn't work after packaging.

Reason: Shelve's open function also loads other database management modules during runtime, so there is no way to know what additional packages are needed, so some necessary modules are missed during packaging.

Workaround: Modify the packing script to add the following:

packages = []
for dbmodule in ['dbhash', 'gdbm', 'dbm', 'dumbdbm']:
try:
__import__(dbmodule)
except ImportError:
pass
else:
# If we found the module, ensure it's copied to the build directory.
(dbmodule)
options = {
'build_exe': {
'includes': 'atexit',
"packages": packages
}
}

4.utilization exceptionOSError: could not get source code

Reason: after packing, they are all pyc files without source code, resulting in dynamic code acquisition failure

Workaround: package the py file containing the required source code into a file, in the 'build_exe' parameter, add 'zip_includes':['path\\]'

Above shared Python cx_freeze packaging tool to deal with the problem ideas and solutions, I hope it will be helpful to you.