Detailed explanation of the method and usage of converting python file into exe file

  • 2021-07-10 20:29:16
  • OfStack

1. Introduction

py2exe is a tool that converts python scripts into standalone executable programs (*. exe) on windows, so that you can run this executable program on windows systems without python.

py2exe has been used to create wxPython, Tkinter, Pmw, PyGTK, pygame, win32com, client and server, and other stand-alone programs. py2exe is released under an open source license.

2. Install py2exe

Download and run installer version of py2exe corresponding to your installed Python from http://prdownloads.sourceforge.net/py2exe, which will install py2exe and corresponding examples; These examples are installed in the lib\ site-packages\ py2exe\ samples directory.

3. Usage of py2exe

If you have an python script called myscript. py and you want to convert it to an executable program running on windows and running it on an windows system without python installed, you should first write a setup script for publishing the program, such as mysetup. py, and insert the statement import py2exe before the setup function.

An example of mysetup. py is as follows:


 # mysetup.py
 from distutils.core import setup
 import py2exe
 setup(console=["myscript.py"])

Then run mysetup. py as follows:

python mysetup.py py2exe

Execution of the above command produces a subdirectory named dist, which contains the files myscript. exe, python 24. dll, library. zip.

If you use compiled C extension modules in your myscript. py script, these modules will also be copied in subdirectories. Similarly, all dll files are required at runtime, except the dll files of the system.

The files in the dist subdirectory contain all the necessary things for your program. You should publish all the contents of this subdirectory.

By default, py2exe creates the following required files under the directory dist:

1, 1 or more exe files.
2. python # #. dll.
3. Several. pyd files, which are compiled extensions, which are required for exe files; Together with the other. dll files, these. dll are required for. pyd.
4. 1 library. zip file containing compiled pure python modules such as. pyc or. pyo

The above mysetup. py creates a console myscript. exe program. If you want to create a GUI program, you only need to mysetup.py In console=["myscript.py"] Replace with windows=["myscript.py"] Yes.

py2exe1 can create multiple exe files. You need to pass the list of these script files to the keyword parameters of console or windows. This is useful if you have several associated scripts.

Run the following command to display all command line flags for the py2exe command.

python mysetup.py py2exe --help

4. Specify additional files

1 Some applications require additional files at run time, such as configuration files, fonts, bitmaps.

If those additional files are specified in the installation script with the data_files option, py2exe can copy them to the dist subdirectory. data_files should contain a list of 1 tuples (target-dir, files) where files is a list of these additional files.

Examples are 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"))],
)

Explanation: The data_files option creates a subdirectory dist\ bitmaps containing two. gif files; 1 subdirectory dist\ fonts, which contains all the. fnt files.

5. Windows NT services

You can build Windows NT services by passing an service keyword parameter to the setup function

The value of this service parameter must be a list of 1 Python module name (including 1service class).

Examples are as follows:


# mysetup.py
from distutils.core import setup
import py2exe
setup(service=["MyService"])

The built executable service can be installed and uninstalled by itself with a command-line parameter tag followed by 1. You can get more help by following this executable service (exe) with the 1-help parameter.

6. COM servers

You can build Windows NT services by passing an com_server keyword parameter to the setup function

The value of this service parameter must be a list of 1 Python module name (containing 1 or more COM server classes).

Examples are as follows:


# mysetup.py
from distutils.core import setup
import py2exe
setup(com_server=["win32com.server.interp"])

By default, DLL and EXE servers are built and you can simply remove them if you don't need them.

Summarize


Related articles: