0% found this document useful (0 votes)
351 views

A Py2exe Tutorial

The document discusses using PyInstaller and Py2Exe to create executable binaries from Python scripts. It provides an example of a simple Py2Exe setup file to bundle a wxPython GUI script into an executable. Advanced Py2Exe setup options for optimization and file bundling are also covered.

Uploaded by

jbsimha3629
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
351 views

A Py2exe Tutorial

The document discusses using PyInstaller and Py2Exe to create executable binaries from Python scripts. It provides an example of a simple Py2Exe setup file to bundle a wxPython GUI script into an executable. Advanced Py2Exe setup options for optimization and file bundling are also covered.

Uploaded by

jbsimha3629
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s.

2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

Home About Contact the Author Follow the Blog License My Projects Sat 31 Jul !1!

A py2exe tutorial Build a Binary Series!


Poste" by Mi#e un"er Pac#aging$ %in"ows &'( Comments ) recei*e" a re+uest to create an article on how to use ,y e-e an" w-Python to create an e-ecutable. ) "$ eci"e" to "o a series on ,ac#aging instea". )t is my intention to go o*er the major %in"ows binary buil"ing utilities an" show you$ "ear rea"er$ how to use them to create a binary that you can "istribute. /nce those articles are "one$ )0ll show how to use )nno an" 1S)S. 2o #ic# things o33$ we0ll go o*er how to use ,y e-e$ ,robably the most ,o,ular o3 the %in"ows e-ecutable ,ac#ages. 6
Tweet

2
shares Share

Lets Get Started

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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

app = wx.App(False) frame = DemoFrame() app.MainLoop()

2his is 3airly straight3orwar"$ so )0ll lea*e it the rea"er to 3igure out. 2his article is about ,y e-e a3ter all.

The py2exe setup.py file


2he #ey to any ,y e-e scri,t is the setup.py 3ile. 2his 3ile controls what gets inclu"e" or e-clu"e"$ how much we com,ress an" bun"le$ an" much more; Here is the sim,lest setu, that we can use with the w- scri,t abo*e9
from distutils.core import setup import py2exe setup(windows=['sampleApp.py'])

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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

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.

Creating an Advan ed setup.py !ile


Let0s see what other o,tions ,y e-e gi*es us 3or creating binaries by creating a more com,le- setu,.,y 3ile.
from distutils.core import setup import py2exe includes = [] excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants', 'Tkinter'] packages = [] dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll', 'tk84.dll'] setup( options = {"py2exe": {"compressed": 2, "optimize": 2, "includes": includes, "excludes": excludes, "packages": packages, "dll_excludes": dll_excludes, "bundle_files": 3, "dist_dir": "dist", "xref": False, "skip_archive": False, "ascii": False, "custom_boot_script": '', } }, windows=['sampleApp.py'] )

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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

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.

"edu ing a #x$ython S ripts Binary Si%e


2here0s a threa" on the w-Python mailing list about re"ucing the siLe o3 the binary. ) contacte" Ste*en S,roat$ one o3 the ,eo,le in the threa"$ about what he "i" an" here0s the tric#9 Set the 4bun"leP3iles5 o,tion to 1 an" the the Li,3ile to 1one. 2he result will be three 3iles in your "ist 3ol"er9 MS=CC'1."ll$ sam,leA,,.e-e an" w7-,o,en.e-e. 2he siLe o3 the 3ol"er is I.7O MB on my machine G2este" on a %in"ows QP machineH. As ) mentione" earlier$ setting bun"leP3iles to 1 can cause some users to e-,erience issues when running your a,,lication$ but this may ha*e been 3i-e" with the newer *ersions o3 ,y e-e. Here0s what the setu,.,y 3ile loo#s li#e with the new o,tions set9
from distutils.core import setup import py2exe includes = [] excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants', 'Tkinter'] packages = [] dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll', 'tk84.dll'] setup( options = {"py2exe": {"compressed": 2, "optimize": 2, "includes": includes, "excludes": excludes, "packages": packages, "dll_excludes": dll_excludes, "bundle_files": 1, "dist_dir": "dist", "xref": False, "skip_archive": False, "ascii": False, "custom_boot_script": '', } }, zipfile = None, windows=['sampleApp.py'] )

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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

&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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

ALSO ON MOUSE VS. THE 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

>

Laurence 3 years ago

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

>

!rog"an 3 years ago

&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

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

$+$ Certifi ation "s../01


iactglobal.in>ProjectPManagement Project Management 2raining )n"ia 3IPEA Certi3icate.77V K-amPass Cate

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

"e ent $osts


Boo# Ce*iew9 Python For Ui"s : A Play3ul )ntro"uction to Programming How to @et a List o3 Class Attributes in Python 2o, 2en Articles o3 !1 Python 1!19 How to Mo*e Files between Ser*ers Central Python K*ents Calen"ar Announce"

"e ent Co--ents


"riscollis on Boo# Ce*iew9 Python For Ui"s : A Play3ul )ntro"uction to Programming ric on Boo# Ce*iew9 Python For Ui"s : A Play3ul )ntro"uction to Programming jma3c on Boo# Ce*iew9 Python For Ui"s : A Play3ul )ntro"uction to Programming "riscollis on Boo# Ce*iew9 Python For Ui"s : A Play3ul )ntro"uction to Programming

8 o3 7

>'> !13 1 91' PM

A ,y e-e tutorial : Buil" a Binary Series; < 2he Mouse =s. 2he Python

htt,9>>www.blog.,ythonlibrary.org> !1!>!'>31>a?,y e-e?tutorial?buil"?a?...

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

Ce*iew Boo#s Con3ig/bj Cross?Plat3orm Eialogs Eistribution Ejango K"ucation @A)


!1! PyCon !11 PyCon
!1

Pac#aging PSF PyCon PyCon

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

%in"ows w-@ri" w-Python QML QML Parsing Series QCC

+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

>'> !13 1 91' PM

You might also like