In this session we have some examples of usage py2exe to convert your python script to a executable file.
1. very simple console.
py2exe.zip
# helloworld.py
n=29
print ” In the Name of God \n”
print ” %i is My Love Number “%29
#——————————————-
#setup.py
import sys, os
from distutils.core import setup
##import glob
import py2exe
setup(console=["helloworld.py"],
options = {“py2exe”:{“compressed”: 1, # Now compress level is 1 if you choose greater than 1 you can not use UPX.
“optimize”: 2,
“ascii”: 1,
“bundle_files”: 1,
“packages”: [] #sometimes you need add the name of module which called in the program.
}},
zipfile = None, # the zip file is’nt created and your program is a single .exe file.
# optimize may break pylab docstring handling
)
Then in DOS Command type
setup.py py2exe
Now you can find helloworld.exe in dist folder. this is a standalone file that you can run it in any windows system without of python. In this program we asked py2exe to not build zipfile, actually zip file contains compiled python .pyc or .pyo. These files can be decompiled to python by depython.net so it is up to you to select which type is better for your case.
2. GUI to exe.
This is a complete example of usage py2exe that you define the icon file, compress level and also manifest . Sometimes after generation, a GUI works god but it looks so bad for example the buttons are not XP type by adding this manifest , you have a program with XP features.
py2exe2.zip
#frame2.py
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY, title, size = (300,300),
style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
page1 = wx.Panel(self,1)
self.bot=wx.Button(page1, -1, “Start”, pos=(0, 0))
self.Bind(wx.EVT_BUTTON, self.printo, self.bot)
def printo(self,event):
wx.MessageBox(“Hello !”)
app = wx.PySimpleApp()
frame1= MainWindow(None, 1,”It is a sample Frame “)
frame1.Show(1)
app.MainLoop()
#——————————————-
#—setup.py—————————-
import sys, os
from distutils.core import setup
import glob
import py2exe
##data = glob.glob(r’d:\Python23\share\matplotlib\*’)
py2exe_options = {“packages”: “encodings,somethingelse”}
manifest = “”"
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<assembly xmlns=”urn:schemas-microsoft-com:asm.v1″
manifestVersion=”1.0″>
<assemblyIdentity
version=”0.64.1.0″
processorArchitecture=”x86″
name=”Controls”
type=”win32″
/>
<description>myProgram</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type=”win32″
name=”Microsoft.Windows.Common-Controls”
version=”6.0.0.0″
processorArchitecture=”X86″
publicKeyToken=”6595b64144ccf1df”
language=”*”
/>
</dependentAssembly>
</dependency>
</assembly>
“”"
“”"
installs manifest and icon into the .exe
but icon is still needed as we open it
for the window icon (not just the .exe)
changelog and logo are included in dist
“”"
setup(windows=[{"script": "frame2.py","icon_resources": [(0, "16.ico")],”other_resources”: [(24,1,manifest)]}],
options = {“py2exe”:{
“compressed”: 1,
“optimize”: 2,
## “ascii”: 1,
“bundle_files”: 1,
“packages”: []}},
zipfile = None , # or none
# optimize may break pylab docstring handling
)