py2exe - generate single executable file
PyInstaller will create a single .exe file with no dependencies; use the --onefile
option. It does this by packing all the needed shared libs into the executable, and unpacking them before it runs, just as you describe (EDIT: py2exe also has this feature, see minty's answer)
I use the version of PyInstaller from svn, since the latest release (1.3) is somewhat outdated. It's been working really well for an app which depends on PyQt, PyQwt, numpy, scipy and a few more.
Create a single-file executable using py2exe
I figured out how to do it using pyinistaller.
Although it makes the exe considerably large, i am happy that i have only single file.
Below is what i did:
- Installed pyinstaller, pywin32
- Open command prompt
- go to my code folder
- use command
pyinstaller --onefile --windowed myframe.py
The manual for pyinstaller has detailed explanations.
py2exe - Create single executable file out of a folder
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.
Compiling python to a single .exe, why is this so difficult?
This question is already answered here:
py2exe - generate single executable file
PyInstaller will create a single .exe file with no dependencies; use the --onefileoption. It does this by packing all the needed shared libs into the executable, and unpacking them before it runs, just as you describe (EDIT: py2exe also has this feature, see minty's answer)
Creating single EXE using py2exe for a Tkinter program
Thanks to this link, you have to edit site-packages/py2exe/build_exe.py
and add "tcl85.dll"
and "tk85.dll"
to the dlls_in_exedir
list. This will get it to run, although you'll still have the tcl
folders, and those two dlls will be there along-side the exe. But it's way better than bundle_files=3
.
self.dlls_in_exedir = [python_dll,
"w9xpopen%s.exe" % (is_debug_build and "_d" or ""),
"msvcr71%s.dll" % (is_debug_build and "d" or ""),
"tcl85.dll",
"tk85.dll"]
Related Topics
How to Calculate Number of Days Between Two Given Dates
Why Can't Python'S Raw String Literals End With a Single Backslash
Python Subprocess Readlines() Hangs
Removing from a List While Iterating Over It
Django Csrf Check Failing With an Ajax Post Request
Does Python Optimize Tail Recursion
Pygame - How to Display Text With Font & Color
Apply Multiple Functions to Multiple Groupby Columns
Should Import Statements Always Be At the Top of a Module
Py2Exe - Generate Single Executable File
How to Use a Variable Inside a Regular Expression
Random String Generation With Upper Case Letters and Digits
Save Plot to Image File Instead of Displaying It Using Matplotlib
How to Replace Nan Values by Zeroes in a Column of a Pandas Dataframe