A Py2exe Tutorial
A Py2exe Tutorial
2he Python
Home About Contact the Author Follow the Blog License My Projects Sat 31 Jul !1!
2
shares Share
For this tutorial$ we0re going to use a w-Python scri,t that "oesn0t "o anything. 2his is a contri*e" e-am,le$ but we0re using w- to ma#e it more *isually interesting than just "oing a console 4Hello %orl"5 ,rogram. 1ote also that ) am using ,y e-e !.6.7$ w-Python .8.11.! an" Python .6. Here0s what the en" ,ro"uct shoul" loo# li#e when run9
1 o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
1ow that we #now what it loo#s li#e$ here0s a loo# at the co"e9
import wx ######################################################################## class DemoPanel(wx.Panel): """""" #---------------------------------------------------------------------def __init__(self, parent): """Constructor""" wx.Panel.__init__(self, parent) labels = ["Name", "Address", "City", "State", "Zip", "Phone", "Email", "Notes"] mainSizer = wx.BoxSizer(wx.VERTICAL) lbl = wx.StaticText(self, label="Please enter your information here:") lbl.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)) mainSizer.Add(lbl, 0, wx.ALL, 5) for lbl in labels: sizer = self.buildControls(lbl) mainSizer.Add(sizer, 1, wx.EXPAND) self.SetSizer(mainSizer) mainSizer.Layout() #---------------------------------------------------------------------def buildControls(self, label): """""" sizer = wx.BoxSizer(wx.HORIZONTAL) size = (80,40) font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD) lbl = wx.StaticText(self, label=label, size=size) lbl.SetFont(font) sizer.Add(lbl, 0, wx.ALL|wx.CENTER, 5) if label != "Notes": txt = wx.TextCtrl(self, name=label) else: txt = wx.TextCtrl(self, style=wx.TE_MULTILINE, name=label) sizer.Add(txt, 1, wx.ALL, 5) return sizer
######################################################################## class DemoFrame(wx.Frame): """ Frame that holds all other widgets """ #---------------------------------------------------------------------def __init__(self): """Constructor""" wx.Frame.__init__(self, None, wx.ID_ANY, "Py2Exe Tutorial", size=(600,400) ) panel = DemoPanel(self) self.Show() #---------------------------------------------------------------------if __name__ == "__main__":
o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
2his is 3airly straight3orwar"$ so )0ll lea*e it the rea"er to 3igure out. 2his article is about ,y e-e a3ter all.
As you can see$ we im,ort the setup metho" 3rom distutils.core an" then we im,ort py2exe. 1e-t we call setu, with a windows #eywor" ,arameter an" ,ass it the name o3 the main 3ile insi"e a ,ython list object. )3 you were creating a non?@A) ,roject$ than you woul" use the console #ey instea" o3 windows. 2o run this$ o,en u, a comman" ,rom,t an" na*igate to the a,,ro,riate location. 2hen ty,e 4,ython setu,.,y ,y e-e5 to run it. 2his is what ) got when ) 3irst ran it9
)t loo#s li#e w-Python re+uires the 4MS=CP7!."ll5 an" %in"ows can0t 3in" it. A +uic# @oogle search yiel"e" the consensus that ) nee"e" the 4Microso3t =isual CBB !!8 Ce"istributable Pac#age5$ 3oun" here. ) "ownloa"e" it$ installe" it an" trie" ,y e-e again. Same error. 2his woul" ha*e ,robably wor#e" ha" ) been using =isual Stu"io to create an e-e o3 a CD ,rogram. Anyway$ the tric# was to search the har" "ri*e 3or the 3ile an" then co,y it to Python0s ELL 3ol"er$ which on my machine was 3oun" at the 3ollowing location9 C9FPython 6FELLs Ga"just as necessary on your machineH. /nce the ELL was in the ,ro,er ,lace$ the setu,.,y 3ile ran just 3ine. 2he result was ,ut into a 4"ist5 3ol"er which contains 1' 3iles an" weighs in at 1I.3 MB. ) "ouble?clic#e" the 4sam,leA,,.e-e5 3ile to see i3 my shiny new binary woul" wor# an" it "i"; )n ol"er *ersions o3 w-Python$ you woul" ha*e nee"e" to inclu"e a mani3est 3ile to get the right loo# an" 3eel Gi.e the themesH$ but that was ta#en care o3 in .8.1! G) thin#H as was the si"e?by?si"e GS-SH assembly mani3est 3ile
3 o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
that use" to be re+uire". 1ote that 3or non?w-Python scri,ts$ you will ,robably still nee" to mess with the S-S mani3ests an" all the hoo,s that inclu"es. Jou can rea" more about that in the ,y e-e tutorial.
2his is ,retty sel3?e-,lanatory$ but let0s un,ac# it anyway. First we set u, a 3ew lists that we ,ass to the o,tions ,arameter o3 the setu, 3unction. 2he includes list is 3or s,ecial mo"ules that you nee" to s,eci3ically inclu"e. Sometimes ,y e-e can0t 3in" certain mo"ules$ so you get to manually s,eci3y them here. 2he excludes list is a list o3 which mo"ules to e-clu"e 3rom your ,rogram. )n this case$ we "on0t nee" 2#inter since we0re using w-Python. 2his list o3 e-clu"es is what @A) K-e will e-clu"e by "e3ault. 2he packages list is a list o3 s,eci3ic ,ac#ages to inclu"e. Again$ sometimes ,y e-e just can0t 3in" something. )0*e ha" to inclu"e email$ PyCry,to$ or l-ml here be3ore. 1ote that i3 the e-clu"es list contains something you0re trying to inclu"e in the ,ac#ages or inclu"es lists$ ,y e-e may continue to e-clu"e them. dll_excludes : e-clu"es "lls that we "on0t nee" in our ,roject. )n the options "ictionary$ we ha*e a 3ew other o,tions to loo# at. 2he compressed #ey tells ,y e-e whether or not to com,ress the Li,3ile$ i3 it0s set. 2he optimize #ey sets the o,timiLation le*el. Mero is no o,timiLation an" is the highest. 2he bundle_files #ey bun"les "lls in the Li,3ile or the e-e. =ali" *alues 3or bundle_files are9 3 N "on0t bun"le G"e3aultH N bun"le e*erything but the Python inter,reter 1 N bun"le e*erything$ inclu"ing the Python inter,reter. A cou,le o3 years ago$ when ) was 3irst learning ,y e-e$ ) as#e" on their mailing list what
O o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
the best o,tion was because ) was ha*ing issues with bun"le o,tion 1. ) was tol" that 3 was ,robably the most stable. ) went with that an" sto,,e" ha*ing ran"om ,roblems$ so that0s what ) currently recommen". )3 you "on0t li#e "istributing more than one 3ile$ Li, them u, or create an installer. 2he only other o,tion ) use in this list is the dist_dir one. ) use it to e-,eriment with "i33erent built o,tions or to create custom buil"s when ) "on0t want to o*erwrite my main goo" buil". Jou can rea" about all the other o,tions Ginclu"ing ones not e*en liste" hereH on the ,y e-e website. By setting o,timiLe to $ we can re"uce the siLe o3 3ol"er by about one megabyte.
Another way to re"uce the siLe o3 the 3ol"er is to use com,ression ,rograms as one o3 my rea"ers$ ProgMan$ ,ointe" out. Here are his results9
I o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
&rapping 'p
Jou now #now the basics 3or creating binaries with ,y e-e. ) ho,e you ha*e 3oun" this hel,3ul 3or your current or 3uture ,rojects. )3 so$ let me #now in the comments;
!urther "eading
2he ,y e-e 2utorial Pac#aging w-PyMail 3or Eistribution @A) K-e w-Python wi#i ,age on ,y e-e < Py%in3 9 @etting %in"ows K*ent Logs R August Pyowa Meeting %ra,u, S
6 o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
RECOMMENDED CONTENT
What's this?
-oo. Re,&ew" P!t$on /or 0& s 1 A P2a!(32 4ntro 3ct&on to ) 5 comments How to 5et a L&st o( C2ass Attr&63tes &n P!t$on
6 comments
Tacos, Trage !, Testosterone" #atc$ E%&so e ' o( Come ! ) Comedy Central * Mone!+Sa,&ng S$o%%&ng T&%s Investopedia
6 comments
D&sc3ss&on
Comm3n&t!
S$are
La3rence
3 years ago
This worked nicely, thanks! But a few comments: The py2exe website (http://www.py2exe.or /index.c ... talks about manifests etc. for !"#$%&'(.dll). *ou +ust say copy "#$%&'(.dll across. ,s this really -./ 0aurence
0
#hare $
r&sco22&s Mo
>
1s mentioned in the article, the last couple releases of wx&ython miti ate the need for the manifest files. 2owe3er, you 4,00 need the manifests if you use &ython 2.56 without wx&ython.
#hare $
ProgMan
3 years ago
usin xp7x85 / python 2.9 / wxpython 2.8.::.( / py2exe (.5.' / ;ad3anced; setup.py, this is what , ot... ...;<dist; folder = :9 files >:?.( "B (:?,585,:5: bytes@A ...9Bip C ;"aximum; compressed ;<dist; folder to = >?.58 "B (?,85D,9?8 bytes@A 1lso, , dropped ;ms3cp'(.dll; in ;<E00s; ahead of time, located ;sampleapp.py; and ;setup.py; in ;<&ython29; root, worked perfect = thanks "ike.
0
#hare $
r&sco22&s Mo
>
&ro man, That;s cool. , know re ular Fippin pro rams can;t compress those files that much. ,;3e used iF1rc and filFip before and , don;t remember the siFe difference bein 3ery substantial. = "ike
0
#hare $
Oscar
3 years ago
' o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
Ar hived (ntry
$ost )ate * Satur"ay$ Jul 31st$ !1! at 19I ,m Category * Pac#aging an" %in"ows Tags * binaries$ Pac#aging$ ,y e-e$ Python$ w-Python )o +ore * Jou can s#i, to the en" an" lea*e a res,onse. Pinging is currently not allowe".
Sear h*
Lin,s
TMouse=sPython Buy the Shirt; Python w-Python
Ar hives
8 o3 7
A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python
jma3c on Boo# Ce*iew9 Python For Ui"s : A Play3ul )ntro"uction to Programming July !1! S + T & T ! S 1 3 O I 6 ' 8 7 1! 11 1 13 1O 1I 16 1' 18 17 ! 1 3 O I 6 ' 8 7 3! 31 < Jun Aug S
Tags
binaries Boo# Pre*iew Boo#
)ronPython
Pyowa
Python
Python 1!1 Python A"*ocacy Python PEF Series Python %eb Framewor#s Py%in3 Cegistry Ce,ortlab siLer S,hin- SWL S+lAlchemy s+lite System A"min 2esting threa"ing 2hrea"s 2)P 2urbo@ears 2utorial
+e-2ers
Log in Kntries CSS Comments CSS %or"Press.org
2he Mouse =s. 2he Python is ,owere" by %or"Press 3.I.1 an" "eli*ere" to you in !.3 6 secon"s using O3 +ueries. 2heme9 Connections Celoa"e" by Ajay EXSouLa. Eeri*e" 3rom Connections.
7 o3 7