Windows7 use Py2exe to package the Python program as an EXE file __python

Source: Internet
Author: User
Tags glob python script


Packaged Environment:



Python version: python-2.7



Operating system: Windows 7



Py2exe version: py2exe-0.6.9.win32-py2.7 (must correspond to the Python version)



Packaging directory: C:\Users\Administrator\Desktop (that is, desktop)






First, Introduction



Py2exe is a tool that converts a Python script into a stand-alone executable program (*.exe) on Windows so that you can run the executable on a Windows system without having to install Python.



Py2exe has been used to create wxpython,tkinter,pmw,pygtk,pygame,win32comclient and servers, and other standalone programs. Py2exe is published under the open source license.



Second, install Py2exe



Download and run the installer that corresponds to the py2exe version of the python you have installed from Http://prdownloads.sourceforge.net/py2exe, which will install Py2exe and the corresponding example; These examples are installed in lib\ The Site-packages\py2exe\samples directory.



Iii. the use of Py2exe



If you have a Python script called helloworld.py, you want to convert it to an executable program running on Windows and run it on a Windows system that doesn't have Python installed. So first you should write a setup script for the release program such as mysetup.py, insert the statement import Py2exe before the setup function.




mysetup.py examples are as follows:



Python code from Distutils.core Import Setup import py2exe Setup (console=["Hellowor ld.py "])



From Distutils.core Importsetup



Import Py2exe






Setup (console=["helloworld.py"])



"Msvcp90.dll:no such file or directory" If the error message is displayed



Please try the following method:



Python code from Distutils.core Import Setup import py2exe Setup (console=["helloworld.py"), options = {"Py2 EXE ": {" dll_excludes ": [" MSVCP90.dll "]}})



From Distutils.core Importsetup



Import Py2exe






Setup



console=["helloworld.py"],



Options = {"Py2exe": {"dll_excludes": ["MSVCP90.dll"]}}



)



Then run mysetup.py in the following way: (Dos:cmd => CD desktop =>mysetup.py py2exe)
Python mysetup.py Py2exe




The above command will produce a subdirectory named Dist that contains the Helloworld.exe,python24.dll,library.zip files.
If your helloworld.py script uses the compiled C extension module, the modules will also be copied in the subdirectory, and all DLL files are required at run time, except for the system DLL file.




The files in the Dist subdirectory contain what your program must do, and you should publish all the contents of this subdirectory together.



By default, Py2exe creates the following required files under Directory dist:
1, one or more EXE files.
2, Python##.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 above mysetup.py creates a console Helloword.exe program, and if you want to create a graphical user interface program, you only need to replace console=["helloworld.py" in mysetup.py with windows= ["myscript.py"] is available.



Py2exe can create multiple EXE files at a time, you need to pass the list of these script files to the console or Windows keyword parameters. If you have several associated scripts, then this is useful.




Run the following command to display all the command line tokens for the Py2exe command.
Python mysetup.py py2exe--help


Python code   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   Options for ' Py2exe '  &nbsp ;   command:--optimize     (-O) optimization level:-o1 for ' Python-o ',-o2 for ' PYTHON&NB sp;   -oo ", and-o0 to disable [default:-o0]--dist-dir     (d) directory to put Fin Al built distributions in (the default is Dist)--excludes     (-e) comma-separated list of modules to exc Lude--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 Li St of modules to include--PACKAGES&Nbsp;    (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 &nbs p;   (-B) bundle DLLs in the ZipFile or the exe. Valid levels are 1, 2, or 3     (default)--skip-archive     do don't place Python b Ytecode files in a archive, put them directly     in the file system--ascii (a)    &N Bsp Do don't automatically include encodings and codecs--custom-boot-script     Python file that'll be RU n When setting up the runtime     environment   usage:     setup_py2exe.py [Global_opts] cmd1 [cmd1_opts] [CMD2 [cmd2_opts].] or:     setup_py2exe.py--help [cmd1 cmd2 ...] o r:     setup_py2exe.py--help-commands or:     setup_py2exe.py cmd--help


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






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 builtdistributions in (default



Is Dist)



--excludes (e) comma-separated List of modules Toexclude



--dll-excludes comma-separated List of DLLs to exclude



--ignores comma-separated List of modules to ignore Ifthey are



Not found



--includes (i) comma-separated List of modules Toinclude



--packages (p) comma-separated List of packages toinclude



--compressed (-c) Create a compressed ZipFile



--xref (-X) Create and show a module crossreference



--bundle-files (-B) bundle DLLs in the ZipFile or the exe. Validlevels



are 1, 2, or 3 (default)



--skip-archive don't place Python bytecode files Inan archive, put



them directly in Thefile system



--ascii (a) does not automatically includeencodings and codecs



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



Runtime environment






Usage:setup_py2exe.py[global_opts] cmd1 [cmd1_opts] [CMD2 [cmd2_opts] ...]



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



or:setup_py2exe.py--help-commands



or:setup_py2exe.py cmd--help






Iv. Designation of additional documents



Some applications require additional files at run time, such as configuration files, fonts, bitmaps.
If you specify those additional files in the setup script with the Data_files option, Py2exe can copy the files to the Dist subdirectory. Data_files should contain a list of tuples (Target-dir, files), where files are a list of these additional files.




Examples are as follows:



Pythoncode: # mysetup.py



Python code from Distutils.core Import Setup import glob import py2exe Setup (console=["helloworld.py"), Data_ files=[("bitmaps", ["Bm/large.gif", "Bm/small.gif"]), ("Fonts", Glob.glob ("Fonts\\*.fnt"))]



From Distutils.core Importsetup



Import Glob



Import Py2exe






Setup (console=["helloworld.py"],



data_files=[("bitmaps",



["Bm/large.gif", "Bm/small.gif"]),



("Fonts",



Glob.glob ("Fonts\\*.fnt")],



)



Description: The Data_files option creates a subdirectory dist\bitmaps containing two. gif files, and a subdirectory dist\fonts that contains all the. fnt files.



V. Windows NT Services



You can build Windows NT services by passing a service keyword parameter to the setup function
, the value of this service parameter must be a list of Python module names (including a service class).




Examples are as follows:



Pythoncode: # mysetup.py



Python code from Distutils.core Import Setup import py2exe Setup (service=["MyService")



From Distutils.core Importsetup



Import Py2exe






Setup (service=["MyService"])



The constructed service can be installed and unloaded from the row after following a certain command line parameter tag. You can get more help with this executable service (EXE) followed by a-help parameter.




VI, COM servers



You can build Windows NT services by passing a com_server keyword parameter to the setup function
, the value of this service parameter must be a list of Python module names (including one or more COM server classes).




Examples are as follows:



Pythoncode: # mysetup.py



Python code from Distutils.core Import Setup import py2exe Setup (com_server=["Win32com.server.interp")



From Distutils.core Importsetup



Import Py2exe






Setup (com_server=["Win32com.server.interp"])



By default, DLLs and EXE servers are built, and you can simply delete them if you don't need them.



A standard py2exe setup file is written



Python code-*-coding:cp936-*-from distutils.core import Setup Import Py2exe = ["includes", "encodings.*"] #Other library files to include options = {"Py2exe": {"Compressed": 1, #compress "optimize": 2, "ASCII": 1, "Includes": Includes, "Bundle_files": 1 #All files are packaged into An exe file}} setup (options = options, Zipfile = none, #do not generate libr Ary.zip file console = [{"script": "hello.py", "Icon_resources": [(1, "Hello.ico" ]}] #Source files, program icon)

-*-coding: cp936-*-

From Distutils.core Importsetup

Import Py2exe

Includes = ["encodings", "encodings. *"]

#Other library files to include

Options = {"Py2exe":

{"Compressed": 1, #Compression

"Optimize": 2,

"ASCII": 1,

"includes": Includes,

"Bundle_files": 1 #Bundle all files into one exe file}

}

Setup

options = options,

Zipfile = none, #Do not generate library. Zip file

console = [{"script": "hello.py", "Icon_resources": [(1, "Hello.ico"]}] #source file, program icon

)

The new version can already be packaged as a file, previously a bunch of dll, pyd. In fact, there is only one place for specific changes. It's the options. Add the Bundle_files entry, the value of 1 means that the PYD and DLL files are packaged into EXE files, and the Python module cannot be loaded from the filesystem; a value of 2 means that PYD and DLL files are packaged into EXE files, but You can load the Python module from the file system. In addition, you can use Zipfile = none in Setup to not generate Library.zip.

For example, the original:

Python code from Distutils.core Import Setup import py2exe includes = ["Encodings", "encodings. *"] Options = {"Py2exe ': {" Compressed ": 1," optimize ": 2," includes ": Includes,} } setup (Version = "0.1.0", Descrip tion = "Search Panda", name = "Search Panda", options = options, windows = [{"script": "search.py", "icon_re Sources": [ (1, "Search.ico")]}],)

From Distutils.core Importsetup

Import Py2exe

Includes = ["encodings", "encodings. *"]

Options = {"Py2exe":

{"Compressed": 1,

"Optimize": 2,

"includes": Includes,

}

}

Setup

Version = "0.1.0",

Description = "Search Panda",

Name = "Search Panda",

options = options,

windows = [{"script": "search.py", "Icon_resources": [(1, "Search.ico")]},

)

Just change to:

Python code

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.