Package python with cx_freeze. exe example-Python tutorial

Source: Internet
Author: User
This article describes how to package python into exe using cx_freeze. For more information, see wxapp. py, read_file.py, and setup. py.

The code is as follows:


#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# File: wxapp. py

Import wx
Import OS
Import sys
Import read_file


Class Frame (wx. Frame ):
Def _ init _ (self ):
Wx. Frame. _ init _ (self, parent = None, title = 'Hello from cx_Freeze ')
Panel = wx. Panel (self)
CloseMeButton = wx. Button (panel,-1, 'close me ')
Wx. EVT_BUTTON (self, closeMeButton. GetId (), self. OnCloseMe)
Wx. EVT_CLOSE (self, self. OnCloseWindow)
PushMeButton = wx. Button (panel,-1, 'Push me ')
Wx. EVT_BUTTON (self, pushMeButton. GetId (), self. OnPushMe)
Sizer = wx. BoxSizer (wx. HORIZONTAL)
Sizer. Add (closeMeButton, flag = wx. ALL, border = 20)
Sizer. Add (pushMeButton, flag = wx. ALL, border = 20)
Panel. SetSizer (sizer)
TopSizer = wx. BoxSizer (wx. VERTICAL)
TopSizer. Add (panel, flag = wx. ALL | wx. EXPAND)
TopSizer. Fit (self)

Def OnCloseMe (self, event ):
Obj = read_file.PrintContent ()
If getattr (sys, 'Frozen ', None ):
Path = OS .path.dirname(sys.exe cutable)
Else:
Path = OS. path. dirname (_ file __)
Path = OS. path. join (path, "read_file.py ")
Obj. show_content (path)

Def OnPushMe (self, event ):
Wx. MessageBox ('I was pushed! ', 'Ionional message ')

Def OnCloseWindow (self, event ):
Self. Destroy ()


Class App (wx. App ):
Def OnInit (self ):
Frame = Frame ()
Frame. Show (True)
Self. SetTopWindow (frame)
Return True


App = App (1)
App. MainLoop ()

The code is as follows:


#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# File: read_file.py

Class PrintContent (object ):
Def show_content (self, path ):
F = open (path)
For line in f:
Print line
F. close ()

The code is as follows:


#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# File: setup. py

# A simple setup script to create an executable running wxPython. This also
# Demonstrates the method for creating a Windows executable that does not have
# An associated console.
#
# Wxapp. py is a very simple 'Hello, world' type wxPython application
#
# Run the build process by running the command 'Python setup. py built'
#
# If everything works well you shoshould find a subdirectory in the build
# Subdirectory that contains the files needed to run the application
Import sys
From cx_Freeze import setup, Executable

Build_exe_options = {"optimize": 2,
"Include_files": ["read_file.py"]}

Base = None
If sys. platform = 'win32 ':
Base = 'win32gu'

Executables = [Executable (script = 'wxapp. py ',
Base = base,
TargetName = "Demo.exe ",
Compress = True,
Icon = "py. ico")]

Setup (name = 'wxapp ',
Version = '0. 1 ',
Description = 'sample cx_Freeze wxPython script ',
Options = {"build_exe": build_exe_options },
Executables = executables)

Open cmd to enter the code directory, and enter:

The code is as follows:


Python setup. py bdist_msi

The build and dist folders are generated. the build folder stores the exe executable files and dependent libraries, directly copy the entire folder to another user, and then double-click the exe file to run it. The dist folder contains the build folder installer and directly transmits the installation package under the dist folder to a friend, after running the installation package, a friend will get the same folder as build. The path is selected by the user.

For the parameter options in setup. py, you can go to the official website to view the corresponding options.

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.