There are many programs that package Python as an exe, such as Py2exe, PyInstaller, and so on, but so far none of them support Python 3.1 well, so I’ll introduce CX_freeze 4.2.2 here, which also supports cross-platform running on Windows and Linux.
Cx_freeze download site is http://sourceforge.net/projects/cx-freeze/files/, want to undertake choosing according to install python version. For example, I am using python3.1, so I will download the file cx_Freeze- 4.2.2.2.win32-py3.1.msi. After running the installation package, the program is copied to the Python directory. There are two main places, one is C:/Python31/Scripts and the other is C:/Python31/Lib/site-packages/cx_Freeze
In previous versions of cx_Freeze, py was converted to exe through the script Freezepython.py, but in 4.2.2 Freezepython.py is gone and the main work is done by the C:/Python 31/scripts cxfree.bat.
There are two main ways to complete python exe packaging using cx_Freeze:
First, run cxfreeze. Bat directly through:
Go to the CMD command line, enter the C:/Python 31/scripts directory, and run cxfreed.bat-h to see its instructions. We can do an experiment with CX_freeze’s own sample.
Go to C :/ Python 31/scripts/ and run
cxfreeze C:/Python31/Lib/site-packages/cx_Freeze/samples/PyQt4/PyQt4app.py –install-dir=d:/123
Pytqt4app.py is packaged as exe, and the libraries it USES are also evaluated in the d:/123 directory.
Second, run setup.py:
In the example provided with CXfreeze C:/ python31/lib /site-packages/cx_Freeze/samples/PyQt4, there is a setup.py file. Open this file and we find that it is:
By running this script, packaging can be done automatically:
For example, if you go into C:/ python31/lib /site-packages/cx_Freeze/samples/PyQt4,
run
setup.py build
After that, a build/exe.win32-3.1 directory will appear under this directory, where you can see the packaged exe file. We want to package our script, copy this setup.py over, and put it in
executables = [Executable(“PyQt4app.py”, base = base)])
Change pyqt4app.py to your own script name.
Problems during packaging:
For Chinese support, if there is Chinese in the script and the encoding format is not specified, the packaging process will occur: UnicodeDecodeError: ‘utf8’ codec can ‘t decode bytes in the position of 1602: invalid data this kind of mistake, this blog post on http://www.cnblogs.com/xinzaitian/archive/2010/12/10/1902481.html, the author mentioned Chinese must be deleted.
In fact, Chinese characters can be retained by adding the following at the beginning of the script file:
#! /usr/bin/env python
#coding=utf-8
It is ok
Another problem is that if the packaged EXE file is run under “desktop” or other Chinese path, a window will pop up showing “Cannot get zipimporter” instance. So far, no good solution has been found