How to package a Python script file into an executable file

Source: Internet
Author: User

How to package a Python script file into an executable file

This article describes how to package Python script files into executable files. This article describes how to package Python2.X and how to package Python3.X. For more information, see

Package the Python script file into an executable file for the following purposes:

1. You can run the software without relying on the Python compiler.

Second, you do not want to publish your source code.

Commonly used tools include py2exe and cx_freeze.

[Tool: py2exe]

Install py2exe

Installing this tool is simple:

Download the corresponding installation program from the official website: http://www.py2exe.org/and click the next step to complete the installation.

After installation, run import py2exe. If no error is reported, the installation is successful!

The Code is as follows:

>>> Import py2exe

>>>

NOTE: Currently, this tool only supports Python2.7. For Python3, you must use another tool: cx_freeze.

Use py2exe

Step 1: Prepare the source code, for example: Hello. py

Step 2: Prepare the compilation script, for example: setup. py

The Code is as follows:

From distutils. core import setup

Import py2exe

Setup (windows = ['hello. py'])

Step 3: run the command: setup. py py2exe

The Code is as follows:

D: \ temp> setup. py py2exe

After running, the dict directory is generated by default under the directory (D: \ temp) that is currently running. The files in the directory are as follows:

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

1. One or more exe files. For example, Hello.exe

2. python ##. dll. In this example: Python27.dll

3.. pyd files, which are compiled extensions and required by 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

Step 4: double-click the hello.exe executable file. The result is the same as that after the source code is run:

Others

1: run setup. py -- help to obtain help information.

The Code is as follows:

Global options:

-- Verbose (-v) run verbosely (default)

-- Quiet (-q) run quietly (turns verbosity off)

-- Dry-run (-n) don't actually do anything

-- Help (-h) show detailed help message

-- No-user-cfg ignore pydistutils. cfg in your home directory

Options for 'py2exe 'command:

-- Optimize (-O) optimization level:-O1 for "python-O",-O2

"Python-OO", and-O0 to disable [default:-O0]

-- Dist-dir (-d) directory to put final built distributions in (default

Is dist)

-- Excludes (-e) comma-separated list of modules to exclude

-- Dll-excludes comma-separated list of DLLs to exclude

-- Ignores comma-separated list of modules to ignore if they are

Not found

-- Includes (-I) comma-separated list of modules to include

-- Packages (-p) comma-separated list of packages to include

-- Compressed (-c) create a compressed zipfile

-- Xref (-x) create and show a module cross reference

-- Bundle-files (-B) bundle dlls in the zipfile or the exe. Valid levels

Are 1, 2, or 3 (default)

-- Skip-archive do not place Python bytecode files in an archive, put

Them directly in the file system

-- Ascii (-a) do not automatically include encodings and codecs

-- Custom-boot-script Python file that will be run when setting up

Runtime environment

Usage: setup. py [global_opts] cmd1 [zookeeper opts] [cmd2 [ipv2_opts]...]

Or: setup. py -- help [cmd1 cmd2...]

Or: setup. py -- help-commands

Or: setup. py cmd -- help

2: a detailed compilation script

Copy the Code as follows:

#-*-Coding: cp936 -*-

From distutils. core import setup

Import py2exe

Includes = ["encodings", "encodings. *"]

Options = {"py2exe ":

{"Compressed": 1, # Compression

"Optimize": 2, # optimization level

"Ascii": 1 ,#

"Primary des": Primary des, # encoding method

"Bundle_files": 1 # All files are packaged into a zipfile or exe file, effective level 1, 2, 3

}}

Setup (

Options = options, # whether the option is required. The default value is None.

Zipfile = None, # Whether to compress the image. The default value is None.

Console = [{"script": "HelloCmd. py", "icon_resources": [(1, "pc. ico")]}], # control port for CMD

Windows = [{"script": "HelloWin. py", "icon_resources": [(1, "pc. ico")]}], # For GUI graphic windows

Data_files = [("magic", ["App_x86.exe",]),],

Version = "v1.01", # version information

Description = "py2exe testing", # description

Name = "Hello, Py2exe", # name Information

)

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.