How Python script files are packaged into executable files

Source: Internet
Author: User
Tags prepare python script

This article mainly introduces the Python script file packaged into an executable file method, this mainly explains the python2.x version of the packaging method, python3.x packaging also has a brief introduction, the need for friends can refer to the

The Python script file is packaged into an executable file for two purposes:

A: No need to rely on the Python compiler to run the software

Two: Do not want to let their own source code announced out

Commonly used tools are: Py2exe, Cx_freeze, etc.

"Tools: Py2exe"

Install Py2exe

Installing the tool is simple:

Only from the official website: http://www.py2exe.org/Download the corresponding version of the installation program, click Next to complete the installation.

After installation, perform import Py2exe, no error indicates the installation is successful!

The code is as follows:

>>> Import Py2exe

>>>

Note: Currently the tool only supports Python2.7, and for Python3, another tool must be used: Cx_freeze

Using Py2exe

The first step: Prepare the source code, if the name is: hello.py

Step two: Prepare to compile the script, if the name is: setup.py

The code is as follows:

From Distutils.core Import Setup

Import Py2exe

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

Step three: 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 in my currently running directory (D:TEMP) with the following files:

By default, Py2exe creates the following required files under Directory dist:

1, one or more EXE files. If this example is: Hello.exe

2, Python##.dll. As in this example: Python27.dll

3.. pyd files, which are compiled extensions that are required by the EXE file, plus other. dll files that are required by. PYD.

4, a Library.zip file that contains the compiled pure Python modules such as. PYc or. pyo

The fourth step: double-click the Hello.exe executable file, the same result as the source code after running:

Other

1: Perform setup.py--help get 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 for

"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 don't place Python bytecode files in a archive, put

them directly in the file system

--ascii (a) does not automatically include encodings and codecs

--custom-boot-script Python file that'll be run when setting up the

Runtime environment

usage:setup.py [global_opts] cmd1 [cmd1_opts] [CMD2 [cmd2_opts] ...]

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

or:setup.py--help-commands

or:setup.py cmd--help

2: A detailed compilation script

Copy code 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, #

"includes": Includes, # encoding method

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

}}

Setup

Options=options, # Do you need an option, default to None

Zipfile=none, # Do you need compression image, default to None

console=[{"script": "hellocmd.py", "Icon_resources": [(1, "Pc.ico")]}, # for CMD control port

windows=[{"script": "hellowin.py", "Icon_resources": [(1, "Pc.ico")]}, # for GUI graphics window

data_files=[("Magic", ["App_x86.exe",]),],

Version = "v1.01", # Versioning information

Description = "Py2exe testing", # Description information

Name = "Hello, Py2exe", # Names Information

)

Related Article

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.