Home > py2exe, python > Python: py2exe setup.py sample

Python: py2exe setup.py sample

Ecco il sample che uso per creare eseguibili con py2exe.

Caratteristiche:
– unico file
– compressione attiva
– icona incorporata

Ho Utilizzato wx per la GUI del fileBrowser (che cerca il
file python sorgente) e la TextEntry per il nome
del file .exe di destinazione.

La dir di destinazione è la solita:
..\dist

Ecco il sourcecode:

#!/usr/bin/python
"""py2exe custom setup Sample
author: bancaldo"""

import py2exe, sys, wx, os, glob
from distutils.core import setup

if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")

class FileBrowser(wx.FileDialog):
    '''Class for file browser'''
    def __init__(self):
        self.fin = None
        wildcard = "File di testo (*.py)|*.py|" \
            "Tutti i files (*.*)|*.*"
        wx.FileDialog.__init__(self, None, "scegli il file", os.getcwd(),
                               "", wildcard, wx.OPEN | wx.CHANGE_DIR)
        if self.ShowModal() == wx.ID_OK:  
            print(self.GetPath())
            self.file = self.GetPath()
            self.fin = open(self.file, 'r')
        else:
            print "operazione apertura annullata"
            self.Destroy()
        self.Destroy()

class Target(object):
    '''Terget'''
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # info di versione
        self.version = "1.0.0"
        self.company_name = "Bancaldo TM"
        self.copyright = "no copyright"
        self.name = "py2exe sample files"


def main():
    '''py2exe setup starter'''
    app = wx.PySimpleApp()
    app.MainLoop()

    manifest_template = '''
    <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
    <assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel level='asInvoker' uiAccess='false' />
          </requestedPrivileges>
        </security>
      </trustInfo>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity 
    	 type='win32' 
    	 name='Microsoft.VC90.CRT' 
    	 version='9.0.21022.8' 
    	 processorArchitecture='*' 
    	 publicKeyToken='1fc8b3b9a1e18e3b' />
        </dependentAssembly>
      </dependency>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity
             type="win32"
             name="Microsoft.Windows.Common-Controls"
             version="6.0.0.0"
             processorArchitecture="*"
             publicKeyToken="6595b64144ccf1df"
             language="*" />
        </dependentAssembly>
      </dependency>
    </assembly>
    '''
    file_to_freeze = FileBrowser().file

    textentry = wx.TextEntryDialog(None, "nome file EXE?", '', '')
    if textentry.ShowModal() == wx.ID_OK:
        destname = textentry.GetValue()
    
    rt_manifest = 24
    
    explicit_incl = ["sqlalchemy.dialects.sqlite.base"]
    other_res = [(rt_manifest, 1, manifest_template % dict(prog = "Rubrica"))]

    setup_opts = {"py2exe": {"includes": explicit_incl,
                             "compressed": 1,
                             "optimize": 2,
                             #"ascii": 1,
                             "bundle_files": 1,}}
    
    test_wx = Target(description = "A GUI app",
                     script = file_to_freeze,
                     other_resources = other_res,
                     icon_resources = [(1, "images\\py.ico")],
                     dest_base = destname)

    # images    
    path = (os.getcwd() + "\\images\\")
    data_files = [('images', [png for png in glob.glob(path + '*.png')])]

    setup(data_files = data_files, # images
          options = setup_opts,
          zipfile = None,
          windows = [test_wx],
        )

if __name__ == '__main__':
    main()
Categorie:py2exe, python
  1. maggio 13, 2013 alle 11:08 am

    We’re a bunch of volunteers and opening a new scheme in our community. Your web site provided us with useful info to work on. You’ve done a formidable process and our entire neighborhood will be grateful to you.

  2. settembre 25, 2013 alle 9:59 am

    Hi, I check your blog on a regular basis. Your
    writing style is awesome, keep it up!

  3. novembre 8, 2013 alle 8:12 am

    Excellent beat ! I wish to apprentice while you amend your website, how could i subscribe for a weblog website?
    The account helped me a acceptable deal. I were tiny bit familiar of this your
    broadcast provided bright transparent concept

  4. marzo 28, 2014 alle 1:55 am

    For most up-to-date information you have to pay a visit internet and on internet I found
    this website as a most excellent web page for most recent
    updates.

  5. aprile 1, 2014 alle 5:07 am

    whoah this weblog is excellent i like studying your articles.
    Keep up the good work! You recognize, a lot of people
    are searching around for this info, you could
    aid them greatly.

  6. aprile 11, 2014 alle 6:11 pm

    Everyone loves it when individuals come together and share ideas.
    Great site, stick with it!

  1. No trackbacks yet.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: