Python tutorial USES py2exe to convert PY files to EXE files

  • 2020-04-02 13:47:19
  • OfStack

A, Introduction to the

Py2exe has been used to create the wxPython, Tkinter, Pmw, PyGTK, pygame, win32com client and server, and other independent program. Py2exe is released under an open source license.

Second, Install py2exe

From http://prdownloads.sourceforge.net/py2exe to download and run the installed with you corresponding py2exe Python version of the installer, which will be installed py2exe and the corresponding example; These examples are installed in the lib \ site - packages \ py2exe \ samples directory.

Three, The use of the py2exe

If you have a python script called myscript.py that you want to convert to an executable running on Windows and running on a 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.
The mysetup.py example 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

After the above command will create a named dist subdirectory, which contains the myscript. Exe, python24. DLL, library. Zip the files.
If your myscript.py script USES compiled C extension modules, these modules will also be copied into subdirectories. Again, all DLLS are required at runtime, except the system's DLL files.
The files in the dist subdirectory contain everything necessary for your program, and you should publish everything in this subdirectory together.

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

1. One or more exe files.
2, python# #. DLL.
3. Several. Pyd files, which are compiled extensions, are required for exe files; Plus the other.dlls that are needed for.pyd.
4. A library.zip file containing compiled pure python modules such as.pyc or.pyo

Mysetup.py above creates a console myscript.exe program. If you want to create a graphical user interface program, you only need to replace the console=["myscript.py"] in mysetup.py with Windows =["myscript.py"].

Py2exe is capable of creating more than one exe file at a time, and you need to pass a list of these script files to the console or Windows keyword parameters. This is useful if you have several scripts associated with it.
Running the following command displays all the command-line tags for the py2exe command.


python mysetup.py py2exe --help

Four, Specify additional files

Some applications require additional files at runtime, such as configuration files, fonts, and bitmaps.
If additional files are optionally specified in the installation script with data_files, then py2exe can copy them to the dist subdirectory. Data_files should contain a list of tuples (target-dir, files) where files are a list of these additional files.

Here's an example:


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

Note: the data_files option will create a subdirectory dist\bitmaps containing two.gif files. A subdirectory, dist fonts, contains all the.fnt files.

Five, Windows NT services

You can build Windows NT services by passing a service keyword argument to the setup function, the value of which must be a list of Python module names (including a service class).

Here's an example:


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

Built executable services can be installed and uninstalled by themselves by following a command line parameter marker. You can get more help by following the -help parameter to the executable service(exe).

Vi. COM servers

You can build Windows NT services by passing a com_server keyword argument to the setup function, the value of which must be a list of Python module names (containing one or more COM server classes).

Here's an example:


# 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.


Related articles: