Package the Python script file into an executable file,

Source: Internet
Author: User

Package the Python script file into an executable file,

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!

>>> 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

from distutils.core import setupimport py2exesetup(windows=['Hello.py'])

Step 3: run the following command:Setup. py py2exe

D:\temp>setup.py py2exe
After running, it will be generated by default under the current directory (D: \ temp) DictDirectory, which contains the following files:


By default, py2exe creates the following files in the directory dist: 1. One or more exe files. For example, Hello.exe 2 and python #. dll. In this example: Python27.dll3 ,. pyd files, which are compiled extensions and required by the exe files. dll files. dll is. 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:RunSetup. py -- helpGet help information

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 directoryOptions 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        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 the                        runtime environmentusage: 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
#-*-Coding: cp936-*-from distutils. core import setupimport py2exeshortdes = ["encodings", "encodings. * "] options = {" py2exe ": {" compressed ": 1, # compression" optimize ": 2, # optimization level" ascii ": 1, #" des ": schemdes, # encoding method "bundle_files": 1 # All files are packaged into a zipfile or exe file. The validity level is 1, 2, 3 }} setup (options = options, # Whether options are 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 ")]}], # windows = [{" script ":" HelloWin. py "," icon_resources ": [(1," pc. ico ")]}], # For the GUI graphic window data_files = [(" magic ", [" App_x86.exe ",]),], version =" v1.01 ", # Version Information description = "py2exe testing", # description information name = "Hello, Py2exe", # name Information)
For more information, see the official documentation:

Http://www.py2exe.org/

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.