This article mainly introduces how to convert a Python program into an exe file using py2exe in Windows. Note that py2exe is only responsible for file format conversion and cannot compile a Python program as a machine code, for more information, see
Prerequisites:
Install the easy-install module, which is a python module packaging tool.
First download the source code of easy_setup.py ,:
Http://pypi.python.org/pypi/setuptools
Use NotePad to store source code. py extension name, which can be executed on the command line, so that you can see several more files about easy_install In the python \ Scripts directory under the Python installation directory. This indicates that easy_install has been installed, how should I use this installation method?
Open cmd, run the command line, and go to the Python \ Scripts directory. Execute easy_install python-dateutil in this directory. you can install the dateutil module and install the pyparsing module.
My machine is:
E:\ANZHUANG\Python\Scripts>easy_install python-dateutil E:\ANZHUANG\Python\Scripts>easy_install pyparsing E:\ANZHUANG\Python\Scripts>easy_install chardet
So the execution is OK!
Install the py2exe module:
Http://prdownloads.sourceforge.net/py2exedownload the corresponding installation package.
Sample Code:
Create a test. py file with the following content:
print "show me"
Create a new mysetup. py compilation file with the following content:
From distutils. core import setup import py2exe setup (console = ["test. py"]) # note that test. py is consistent with the previous one.
Run the following command:
>>python mysetup.py py2exe
Running result:
A new dist directory under the current directory contains unequal files of the following types:
- . Exe contains a directory file
- . Pyd compiled py file
- . Dll external DLL file, which contains a pythonxx. dll
- The library files required for. zip are packed with all the compilation files for the library files.
View help:
python mysetup.py py2exe --help
Example directory:
After the py2exe module is installed, the built-in samples are stored in: lib \ site-packages \ py2exe \ samples.
Note:
The parameters required for Compiling different types of binary are different. For example, the parameter name of the command line program is console, while that of the GUI program is Windows; for details, refer to the sample or HELP command script first!
Background:
I thought that only one exe file was left after the conversion. I used it to find that I didn't want to perform binary encoding for all the content as I imagined. Well, sometimes you think too well. After all, this is the easiest way to achieve it!