A detailed approach to Python packaging executable files _python

Source: Internet
Author: User

The examples in this article describe how Python packages executables. Share to everyone for your reference, specific as follows:

Python programs need to rely on a native-installed Python library, if you want to run on a machine that does not have Python installed, you need to package the distribution, there are currently two more useful tools: Pyinstaller and Py2exe. Where Py2exe is applied under Windows, and Pyinstall can be applied to Windows, Linux, and Mac OS X.

Here is only a piece of Py2exe packaged instance code. (Py2exe download address)

#coding =utf-8
from distutils.core Import setup
import py2exe
includes = ["Encodings", "encodings.*"]
#要包含的其它库文件
options = {"Py2exe":
  {"
    Compressed": 1, #压缩
    "optimize": 2,
    "ASCII": 1,
    "includes ": Includes,
    " Bundle_files ": 1 #所有文件打包成一个exe文件
  }
}
Setup (
  options = options,
  zipfile= None,  #不生成library. zip file
  console=[{"script": "main.py", "Icon_resources": [(1, "Q.ico")]}] #源文件, program icon
)

If the above source code is saved as mysetup.py, the Package command is: Python mysetup.py py2exe.

PS: The Use of Enigma Virtual box can also make Py2exe packaging out of the single file More perfect. Enigma Virtual Box can click here to download: http://www.jb51.net/softs/425055.html

Two used instances:

(1), background operation

#coding =utf-8
from distutils.core Import setup
import py2exe
includes = ["Encodings", "encodings.*"]
#要包含的其它库文件
options = {"Py2exe":
  {"
    Compressed": 1, #压缩
    "optimize": 2,
    "ASCII": 1,
    "includes ": Includes,
    " Bundle_files ": 1 #所有文件打包成一个exe文件
  }
}
Setup (
  options = options,
  zipfile= None,  #不生成library. zip file
  windows=[{"script": "main.py"}] #源文件
)

(2), have to have a console window, and can not be packaged as an EXE file. (Walker is because of the call to the browser)

#coding =utf-8 from
distutils.core Import setup
import py2exe
setup (
  console = [{' Script ':] Main.py "}],
  options={
      " Py2exe ": {
          " skip_archive ": True,
          " unbuffered ": true,
          " optimize ": 2
      }
  }
)

PS: About using Pyinstalle to pack python scripts into exe files, refer to this site article: http://www.jb51.net/article/88235.htm

More information about Python-related content can be viewed in this site: "Python file and directory operation tips Summary", "Python text file Operation tips Summary", "Python URL operation tips Summary", "Python Picture Operation tips Summary", " Python data structure and algorithm tutorial, Python socket Programming Tips Summary, Python function Usage tips Summary, python string manipulation tips and Python introductory and advanced classic tutorials

I hope this article will help you with Python programming.

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.