First, introduction
Py2exe has been used to create wxpython,tkinter,pmw,pygtk,pygame,win32com client and server, and other standalone programs. Py2exe is published under the open source license.
Second, install Py2exe
Download and run the installer that corresponds to the py2exe version of the python you have installed from Http://prdownloads.sourceforge.net/py2exe, which will install Py2exe and the corresponding example; These examples are installed in lib\ The Site-packages\py2exe\samples directory.
Iii.The use of Py2exe
If you have a Python script called myscript.py, you want to convert it to an executable program running on Windows and run it on a Windows system that doesn't have Python installed. So first you should write a setup script for the release program such as mysetup.py, insert the statement import Py2exe before the setup function.
mysetup.py examples are as follows:
Copy Code code as follows:
# mysetup.py
From Distutils.core Import Setup
Import Py2exe
Setup (console=["myscript.py"])
Then run mysetup.py in the following way:
Copy Code code as follows:
The above command will produce a subdirectory named Dist that contains the Myscript.exe,python24.dll,library.zip files.
If your myscript.py script uses the compiled C extension module, the modules will also be copied in the subdirectory, and all DLL files are required at run time, except for the system DLL file.
The files in the Dist subdirectory contain what your program must do, and you should publish all the contents of this subdirectory together.
By default, Py2exe creates the following required files under Directory dist:
1, one or more EXE files.
2, Python##.dll.
3. Pyd files, which are compiled extensions that are required by the EXE file, plus other. dll files that are required by. PYD.
4, a Library.zip file that contains the compiled pure Python modules such as. PYc or. pyo
The above mysetup.py creates a console Myscript.exe program, and if you want to create a graphical user interface program, you only need to replace console=["myscript.py" in mysetup.py with windows=[. Myscript.py "] both available.
Py2exe can create multiple EXE files at a time, you need to pass the list of these script files to the console or Windows keyword parameters. If you have several associated scripts, then this is useful.
Run the following command to display all the command line tokens for the Py2exe command.
Copy Code code as follows:
Python mysetup.py py2exe--help
Iv. Designation of additional documents
Some applications require additional files at run time, such as configuration files, fonts, bitmaps.
If you specify those additional files in the setup script with the Data_files option, Py2exe can copy the files to the Dist subdirectory. Data_files should contain a list of tuples (Target-dir, files), where files are a list of these additional files.
Examples are as follows:
Copy Code code as follows:
# mysetup.py
From Distutils.core Import Setup
Import Glob
Import Py2exe
Setup (console=["myscript.py"],
data_files=[("bitmaps",
["Bm/large.gif", "Bm/small.gif"]),
("Fonts",
Glob.glob ("Fonts\\*.fnt")],
)
Description: The Data_files option creates a subdirectory dist\bitmaps containing two. gif files, and a subdirectory dist\fonts that contains all the. fnt files.
V.Windows NT Services
You can build Windows NT services by passing a service keyword parameter to the setup function, and the value of this service parameter must be a list of Python module names (including a service class).
Examples are as follows:
Copy Code code as follows:
# mysetup.py
From Distutils.core Import Setup
Import Py2exe
Setup (service=["MyService"])
The constructed service can be installed and unloaded from the row after following a certain command line parameter tag. You can get more help with this executable service (EXE) followed by a-help parameter.
VI,COM servers
You can build Windows NT services by passing a com_server keyword parameter to the setup function, and the value of this service parameter must be a list of Python module names that contain one or more COM server classes.
Examples are as follows:
Copy Code code as follows:
# mysetup.py
From Distutils.core Import Setup
Import Py2exe
Setup (com_server=["Win32com.server.interp"])
By default, DLLs and EXE servers are built, and you can simply delete them if you don't need them.