Python Tutorial: convert a PY file to an EXE file using py2exe

Source: Internet
Author: User
Tags glob
Py2exe is an executable program (*. in this way, you can run this executable program on windows without installing python. I, Introduction

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

II,Install py2exe

Slave.

III,Py2exe usage

If you have a script named myscript. you want to convert the python script of py to an executable program running on windows and run it on a windows system without python installed, first, you should write a setup script for the release program, such as mysetup. py, insert the statement import py2exe before the setup function.
An example of mysetup. py is as follows:

The code is as follows:


# Mysetup. py
From distutils. core import setup
Import py2exe

Setup (console = ["myscript. py"])

Run mysetup. py as follows:

The code is as follows:


Python mysetup. py py2exe

Upload these files.
If your myscript. if the compiled C extension module is used in the py script, these modules will be copied to sub-directories. Similarly, all dll files are required at runtime, besides System dll files.
The files in the dist subdirectory contain what is necessary for your program. you should publish all the content in this subdirectory together.

By default, py2exe creates the following necessary files in the dist Directory:

1. one or more exe files.
2. python ##. dll.
3. Several. pyd files, which are compiled extensions and required by the exe files. with other. dll files, these. dll files are required by. pyd.
4. a library.zip file that contains compiled python modules such as. pyc or. pyo

Mysetup.pycreated a console myscript.exe program. if you want to create a program in the graphic user field, you only need to set mysetup. console = ["myscript. replace py "] with windows = [" myscript. py.

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. This is useful if you have several associated scripts.
Run the following command to display all command line labels of the py2exe command.

The code is as follows:


Python mysetup. py py2exe -- help

IV,Specify additional files

Some applications require additional files such as configuration files, fonts, and bitmaps when running.
If you specify the additional files with the data_files option in the installation script, py2exe can copy these files to The dist subdirectory. Data_files should contain a list of target-dir, files, where files is a list of these additional files.

Example:

The code is 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")],
)

Note: The data_files option will create a sub-directory dist \ bitmaps, which contains two. gif files; a sub-directory dist \ fonts, which contains all. fnt files.

V,Windows NT services

You can construct 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.

Example:

The code is as follows:


# Mysetup. py
From distutils. core import setup
Import py2exe
Setup (service = ["MyService"])

The executable service can be installed and uninstalled by marking it with certain command line parameters. You can get more help by following the executable service (exe) with the-help parameter.

VI,COM servers

You can pass a com_server keyword parameter to the setup function to construct Windows NT services. The value of this service parameter must be a Python module name (including one or more COM server classes).

Example:

The code is 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. you can simply delete them without them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.