Kadjo

SoftDev Journal

About This Journal

24 June 2007 by Jojo Makiling

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

January 24th, 2007 by pepesmith
    1. Overview
      This is a simple howto on generating stand-alone executables for pygtk programs using py2exe-0.6.5 & PyGtk 2.10.3.
    2. 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.
    3. 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 glob

      opts = {
          "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")],
      )

    4. 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.
    5. 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.
    6. 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 Sections

      Section "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 Section

      Section "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.

Posted in Python | 3 Comments »

3 Responses

  1. criando um .exe com Python + GTK « ldev Says:

    [...] MAS ATENÇÃO, para o programa funcionar em outros computadores , o GTK deverá estar instalado!!! Se você quiser embutir o GTK, siga esse tuto [...]

  2. Liu Zhongshu » Blog Archive » 使用py2exe Says:

    [...] py2exe是在python的distutil基础上使用的,因此用起来很简单,这里有一个简单的教程: http://www.kadjo.net/softdev/?p=56不过由于gtk有些庞大,作者使用了一些exclude语句,是为了把gtk运行库不放在最终的dist目录下,但实际上放也是可以的,只是需要更多一些的空间(gtk运行库加上python绑定还是蛮大的)。 [...]

  3. Liu Zhongshu » Blog Archive » ??py2exe Says:

    [...] http://www.kadjo.net/softdev/?p=56 [...]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.