How to package a Python script file into an executable file

Source: Internet
Author: User
The Python script file is packaged as an executable file for two purposes:

A: You do not need to rely on the Python compiler to run the software
Two: Do not want to let their own source published

Commonly used tools are: Py2exe, Cx_freeze, etc.

"Tools: Py2exe"

Installing Py2exe
Installing the tool is simple:
Just download the installer for the version from the official website: http://www.py2exe.org/and click Next to complete the installation.
After installation, the implementation of import Py2exe, no error indicates that the installation was successful!
Copy the Code code as follows:


>>> Import Py2exe
>>>

Note: Currently this 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 the compilation script, if the name is: setup.py
Copy the Code code as follows:


From Distutils.core Import Setup
Import Py2exe

Setup (windows=[' hello.py ')

Step three: Run the command: setup.py py2exe
Copy the Code code as follows:


d:\temp>setup.py Py2exe

After running, the Dict directory is generated by default in the directory I am currently running (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, they are required by EXE files, plus other. dll files, which are required by the. PYD.
4. A library.zip file that contains compiled pure Python modules such as. PYc or. pyo

Fourth step: Double-click the Hello.exe executable file, followed by the same result as the source code:

Other

1: Execute setup.py--help get help information
Copy the Code code 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 is
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
is 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 would be is 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 the 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
"Bundle_files": 1 # All files packaged into one zipfile or EXE file, effective level
}}
Setup
Options=options, # Whether you need an optional option, default is None
Zipfile=none, # Whether to 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", # Release information
Description = "Py2exe testing", # Description information
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.