Python's method for packaging executable files

  • 2020-05-12 02:47:56
  • OfStack

The example in this article shows how Python packages an executable. I will share it with you for your reference as follows:

The Python program relies on the locally installed Python library, and if you want to run it on a machine that doesn't have Python installed, you'll need to package and distribute it. There are two easy tools available: PyInstaller and py2exe. Among them, py2exe is applied under windows, while PyInstall can be applied on windows, Linux and Mac OS X.

Only one piece of py2exe packaged instance code is posted here. (py2exe download address)


#coding=utf-8
from distutils.core import setup
import py2exe
includes = ["encodings", "encodings.*"]
# Other library files to include 
options = {"py2exe":
  {
    "compressed": 1, # The compression 
    "optimize": 2,
    "ascii": 1,
    "includes": includes,
    "bundle_files": 1 # All files are packaged into 1 a exe file 
  }
}
setup (
  options = options,
  zipfile=None,  # Does not generate library.zip file 
  console=[{"script": "main.py", "icon_resources": [(1, "Q.ico")] }]# Source file, program icon 
)

If the above source is saved as mysetup.py, the packaging command is python mysetup.py py2exe.

PS: using Enigma Virtual Box also makes Py2exe single files more perfect. Enigma Virtual Box can click here to this site to download: / / www ofstack. com softs / 425055. html

Two used examples:

(1) background operation


#coding=utf-8
from distutils.core import setup
import py2exe
includes = ["encodings", "encodings.*"]
# Other library files to include 
options = {"py2exe":
  {
    "compressed": 1, # The compression 
    "optimize": 2,
    "ascii": 1,
    "includes": includes,
    "bundle_files": 1 # All files are packaged into 1 a exe file 
  }
}
setup (
  options = options,
  zipfile=None,  # Does not generate library.zip file 
  windows=[{"script": "main.py" }]# The source file 
)

(2) it is necessary to have a console window and cannot be packaged as one exe file. (walker because it called 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 Python script packaged into exe file, refer to this article: / / www ofstack. com article / 88235. htm

More about Python related topics: interested readers to view this site "Python file and directory skills summary", "Python skills summary text file", "Python URL skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using skills summary", "Python string skills summary" and "Python introductory and advanced tutorial"

I hope this article is helpful to you Python programming.


Related articles: