About converting Python files to exe files

Source: Internet
Author: User
Tags glob

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.

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 myscript.py, you want to convert it to an executable program running on Windows and run 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, which inserts the statement import Py2exe before the setup function.
The mysetup.py example is as follows:

1 # mysetup.py 2  from Import Setup 3 Import Py2exe 4 Setup (console=["myscript.py"])


Then run the mysetup.py as follows:

Python mysetup.py Py2exe

The above command will produce a subdirectory named Dist, which contains Myscript.exe, Python24.dll, library.zip these files.
If your myscript.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:
1, one or more EXE files.
2, Python##.dll.
3. Several. pyd files, which are compiled extensions that are required by the EXE file, plus other. dll files that are required by the. PYD.
4. A library.zip file that contains 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 graphical user interface program, then you just need to mysetup.py console=["myscript.py" replaced with windows=[" myscript.py "] can either.

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.py fromDistutils.coreImportSetupImportGlobImportPy2exesetup (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 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.py
From Distutils.core Import Setup
Import Py2exe

Setup (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.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.

About converting Python files to exe files

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.