Reprinted from xiake200704 final editor xiake200704
First, Introduction
Py2exe is a tool for converting a Python script into a standalone executable program (*.exe) on Windows so that you can run the executable program on a Windows system without having to install Python.
Py2exe has been used to create wxpython,tkinter,pmw,pygtk,pygame,win32com client and server, and other standalone programs. The Py2exe is released under an open source license. At present, only python2.x of the corresponding version.
Second, installation Py2exe
Download and run the Py2exe version of the installer that corresponds to the python you installed from Http://prdownloads.sourceforge.net/py2exe, which installs Py2exe and the corresponding example, which are installed in lib\ The Site-packages\py2exe\samples directory.
Third, the use of Py2exe
If you have a Python script named helloworld.py that you want to convert to an executable program running on Windows and run on a Windows system that does not have Python installed, first you should write a setup script for the publisher such as mysetup.py, where SE The Tup function is inserted before the statement import Py2exe.
The mysetup.py example is as follows:
| 12345 |
# mysetup.pyfromdistutils.core import setupimport py2exesetup(console=["helloworld.py"]) |
Console to Windows will package the. pyw file and will not produce a cmd-like window
Then run the mysetup.py as follows:
When the above command executes, it will produce a subdirectory named Dist that contains the Helloworld.exe,python24.dll,library.zip files.
If your helloworld.py script uses compiled C extensions, the modules are also copied in the subdirectory, and all DLL files are required at run time, in addition to the system's DLL files.
The files in the Dist subdirectory contain what is necessary for your program, and you should publish all the content in this subdirectory.
By default, Py2exe creates the following required files under Directory dist:
- One or more EXE files.
- Python##.dll.
- Several. pyd files, which are compiled extensions that are required by the EXE file, plus other. dll files that are required by the. PYD.
- A library.zip file that contains compiled pure Python modules such as. PYc or. pyo
The above mysetup.py creates a console for the Helloword.exe program, if you want to create a graphical user interface program, then you just need to mysetup.py console=["helloworld.py" replaced with windows= ["myscript.py"] can.
Py2exe You can create multiple EXE files at once, you need to pass a list of these script files to the console or Windows keyword parameters. This is useful if you have several associated scripts.
Run the following command to display all the command line tokens for the Py2exe command.
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 additional files are specified in the installation 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:
# mysetup.pyfromdistutils.core importsetupimportglobimportpy2exesetup( console=["helloworld.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 that contains 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
, the value of this service parameter must be a list of Python module names (including a service class).
Examples are as follows:
# mysetup.pyfromdistutils.core import setupimport py2exesetup(service=["MyService"]) |
The built-in service can be installed and uninstalled from the line by following a certain command-line parameter tag after it. 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
, 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:
# mysetup.pyfromdistutils.core import setupimport py2exesetup(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.
Another: The code for a standard setup.py is as follows:
#!/usr/bin/python#filename:setup.py#coding=utf-8fromdistutils.core importsetupimportpy2exeincludes =["encodings", "encodings.*"]#要包含的其它库文件options ={ "py2exe": { "compressed": 1, #压缩 "optimize": 2, "ascii": 1, "includes":includes, "bundle_files": 1#所有文件打包成一个exe文件 }}setup( version ="XXX", description ="XXX", name ="XXX", options =options, zipfile=None, #不生成library.zip文件 console=[ { "script": "hello.py", "icon_resources": [(1, "hello.ico")] } ]#源文件,程序图标) |
Python Turn Exe2