About This Journal
Technical concerns tend to find a solution as long as there are good people working on them. And Linux has the very best. - Linus Torvalds
PyGTK and Py2Exe HowTo
pepesmith
- Overview
This is a simple howto on generating stand-alone executables for pygtk programs using py2exe-0.6.5 & PyGtk 2.10.3. - Initial Installation (Windows OS)
1) Install the latest version of python. You may need to add the install location to your PATH environment variable...
e.g. c:\python24
2) Install the latest version of GTK+ for windows.
3) Install the latest version of pygtk for windows.
4) Install py2exe 0.5.x or later.
You may install the installer of Alberto Ruiz (arc) : http://osl.ulpgc.es/~arc/gnome/pygtk-setup.exe
This is all in one installer though still there is a need for refinement on this.
5) Install the installer NSIS. - Configure py2exe for your project
1) Build a setup.py file.
I'm using this on my project - the pytodolist# setup.py
from distutils.core import setup
import py2exe
import globopts = {
"py2exe": {
"excludes": "pango,atk,gobject",
"dll_excludes": [
"iconv.dll","intl.dll","libatk-1.0-0.dll",
"libgdk_pixbuf-2.0-0.dll","libgdk-win32-2.0-0.dll",
"libglib-2.0-0.dll","libgmodule-2.0-0.dll",
"libgobject-2.0-0.dll","libgthread-2.0-0.dll",
"libgtk-win32-2.0-0.dll","libpango-1.0-0.dll",
"libpangowin32-1.0-0.dll"],
}
}setup(
name = "PyToDoList",
description = "A simple ToDoList Program",
version = "0.61",
windows = [
{"script": "todo.py",
"icon_resources": [(1, "checker1.ico")]
}
],
options=opts,
data_files=[("todo.db"),("jojopix.png"),("checker1.ico")],
) - Build the executable
From a command prompt in your project_name folder, run:
python setup.py py2exe
This should create a dist folder in your project folder. - GTK+ Runtime files
A GTK+ runtime for windows will also need to be installed for the standalone executable. Get that from here.
If you happened to used the installer of Alberto Ruiz (arc). You dont have to install this one. - Compiling the Installer using NSIS.
Usually the file extension of the NSI script is .nsi. For more information on NSIS scripting, there's a tutorial available at sourceforge site. Below is my sample NSIS script:
;--------------------------------
;Include Modern UI!include "MUI.nsh"
;--------------------------------
;General;Name and file
Name "PyToDoList"
OutFile "PyToDoList.exe";Default installation folder
InstallDir "$PROGRAMFILES\PyToDoList"
;Get installation folder from registry if available
InstallDirRegKey HKCU "Software\PyToDoList" "";--------------------------------
;Interface Settings!define MUI_ABORTWARNING
;--------------------------------
;Pages!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "..\License.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English";--------------------------------
;Installer SectionsSection "Package" SecDummy
SetOutPath "$INSTDIR"
File "..\dist\*.*"
;Store installation folder
WriteRegStr HKCU "Software\PyToDoList" "" $INSTDIR
;Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"
CreateShortCut "$INSTDIR\PyToDoList.lnk" "$INSTDIR\todo.exe"
SetOutPath "$SMPROGRAMS\MegSoft Solutions\"
CopyFiles "$INSTDIR\PyToDoList.lnk" "$SMPROGRAMS\MegSoft Solutions\"
CopyFiles "$INSTDIR\PyToDoList.lnk" "$DESKTOP\"
Delete "$INSTDIR\PyToDoList.lnk"
CreateShortCut "$SMPROGRAMS\GemSoft Solutions\Uninstall.lnk" "$INSTDIR\Uninstall.exe"SectionEnd
Section "GTK+" SecDummy2
SetOutPath "$INSTDIR\"
File "..\..\gtklib\*.dll"SetOutPath "$INSTDIR\etc\fonts\"
File "..\..\gtklib\etc\fonts\*.*"SetOutPath "$INSTDIR\etc\gtk-2.0\"
File "..\..\gtklib\etc\gtk-2.0\*.*"SetOutPath "$INSTDIR\etc\pango\"
File "..\..\gtklib\etc\pango\*.*"SetOutPath "$INSTDIR\lib\gtk-2.0\2.4.0\loaders\"
File "..\..\gtklib\lib\gtk-2.0\2.4.0\loaders\*.*"SetOutPath "$INSTDIR\lib\gtk-2.0\2.4.0\engines\"
File "..\..\gtklib\lib\gtk-2.0\2.4.0\engines\*.*"SetOutPath "$INSTDIR\lib\gtk-2.0\2.4.0\immodules\"
File "..\..\gtklib\lib\gtk-2.0\2.4.0\immodules\*.*"SetOutPath "$INSTDIR\lib\pango\1.4.0\modules\"
File "..\..\gtklib\lib\pango\1.4.0\modules\*.*"SetOutPath "$INSTDIR\share\themes\Default\gtk-2.0\"
File "..\..\gtklib\share\themes\Default\gtk-2.0\*.*"SetOutPath "$INSTDIR\share\themes\MS-Windows\gtk-2.0\"
File "..\..\gtklib\share\themes\MS-Windows\gtk-2.0\*.*"SectionEnd
;--------------------------------
;Descriptions;Language strings
LangString DESC_SecDummy ${LANG_ENGLISH} "Main Package"
LangString DESC_SecDummy2 ${LANG_ENGLISH} "GTK+ Package. To be installed if you dont have pre installed GTK+";Assign language strings to sections
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
!insertmacro MUI_DESCRIPTION_TEXT ${SecDummy2} $(DESC_SecDummy2)
!insertmacro MUI_FUNCTION_DESCRIPTION_END;--------------------------------
;Uninstaller SectionSection "Uninstall"
Delete "$INSTDIR\*.*"
Delete "$DESKTOP\PyToDoList.lnk"
Delete "$SMPROGRAMS\GemSoft Solutions\PyToDoList.lnk"
Delete "$SMPROGRAMS\GemSoft Solutions\Uninstall.lnk"RMDir "$SMPROGRAMS\GemSoft Solutions\"
RMDir /r "$INSTDIR\etc\"
RMDir /r "$INSTDIR\lib\"
RMDir /r "$INSTDIR\share\"RMDir "$INSTDIR"
DeleteRegKey /ifempty HKCU "Software\PyToDoList"
SectionEnd
There are two options for compiling .nsi file.
- Compile the .nsi file by right clicking it and select the Compile NSIS. The
output exe is the installer of the file. - Issue the command makensis
. This will then generate the output exe file.
- Compile the .nsi file by right clicking it and select the Compile NSIS. The
Posted in Python |
3 Comments »
3 Responses
Leave a Comment
January 7th, 2008 at 2:07 pm
[...] MAS ATENÇÃO, para o programa funcionar em outros computadores , o GTK deverá estar instalado!!! Se você quiser embutir o GTK, siga esse tuto [...]
January 26th, 2008 at 12:33 am
[...] py2exe是在pythonçš„distutilåŸºç¡€ä¸Šä½¿ç”¨çš„ï¼Œå› æ¤ç”¨èµ·æ¥å¾ˆç®€å•,这里有一个简å•的教程: http://www.kadjo.net/softdev/?p=56ä¸è¿‡ç”±äºŽgtk有些庞大,作者使用了一些excludeè¯å¥ï¼Œæ˜¯ä¸ºäº†æŠŠgtkè¿è¡Œåº“䏿”¾åœ¨æœ€ç»ˆçš„dist目录下,但实际上放也是å¯ä»¥çš„ï¼Œåªæ˜¯éœ€è¦æ›´å¤šä¸€äº›çš„空间(gtkè¿è¡Œåº“åŠ ä¸Špython绑定还是蛮大的)。 [...]
December 15th, 2009 at 5:48 pm
[...] http://www.kadjo.net/softdev/?p=56 [...]