Frage

Hello Stackoverflowers,

I have been trying to create a single executable file out of a folder which contains a python script and some other modules and files (note: I am also using Tkinter if that is important).

I have already looked at a lot of questions/answers relating to this and tried them but none of them seem to work for me.

Here is what my folder looks like:

python-calendar                #base-folder
|
|___apliclient                 #module
|___httplib2                   #module
|___oauth2client               #module
|___uritemplate                #module
|___client_secrets.json        #used by program.py
|___program.py                 #my main script
|___program.dat                #updated by program.py

My question is:
How can I create a single executable file that groups all these files/folders together into a single, standalone executable file that can be run?

This is what my setup.py file looks like for the py2exe installer at the moment however when I run my executable, nothing happens (both from the folder and from command-line).

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

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True, "includes" : ['apiclient','httplib2','oauth2client',
                        'uritemplate'] }},
    console = ["program.py"],
    zipfile = None,
    data_files=['client_secrets.json']
)

Any guidance on how to use py2exe for this or any other executable creator would be really helpful. Thank you in advance.

War es hilfreich?

Lösung

I found the solution in part. First, modify your setup.py like this:

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

sys.argv.append('py2exe')
PROGRAM_DAT     = open('program.dat').read()
CLIENT_SECRETS  = open('client_secrets.json').read()

setup(windows=[{'script': "program.py",
            'other_resources': [
                 (u'PROGRAM_DAT', 1, PROGRAM_DAT), 
                 (u'CLIENT_SECRETS', 2, CLIENT_SECRETS) 
            ]
           }],
            options = {'py2exe': {'bundle_files': 1, 'compressed': True, "includes" : ['apiclient','httplib2','oauth2client',
                    'uritemplate']}
           },

           zipfile = None
 )

If you want to build console application, simply change setup(windows= on setup(console=.

In program.py you can load resources like this:

import win32api
from StringIO import StringIO

datfile = StringIO( win32api.LoadResource(0, u'PROGRAM_DAT', 1))
print datfile.getvalue()

secrets = StringIO( win32api.LoadResource(0, u'CLIENT_SECRETS', 2))
print secrets.getvalue()

But there is no way to modify program.exe from program.exe. To save your changes in embedded program.dat, you will need another exe-file. Then, you can use win32api.BeginUpdateResource, win32api.UpdateResource and win32api.LoadResource functions.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top