Mostrando las entradas con la etiqueta py2exe. Mostrar todas las entradas
Mostrando las entradas con la etiqueta py2exe. Mostrar todas las entradas

viernes, octubre 29, 2010

Como generar archivos .exe e instaladores para una aplicación python (y pygtk)

Como generar archivos .exe e instaladores para una aplicación python


Este documento describe los pasos necesarios para crear un archivo ejecutable
de una aplicación python y como generar un instalador y una versión portable
para dicha instalación.

Este documento asume que la aplicación se basa en GTK pero debería funcionar
con menores cambios en otros toolkits.

porque un instalador

  • se requiere instalar muchos componentes a mano por el usuario final para una sola aplicación
  • muchos instaladores pequeños
  • difíciles de encontrar
  • difícil encontrar las versiones exactas que funcionan en conjunto
  • requiere instalarlos en un orden definido
  • rezar
  • algunas veces incluso haciendo todo bien puede no funcionar
  • fácil de automatizar y documentar para replicar con cada nueva versión
  • liberar al usuario final de los problemas para poder usar la aplicación

componentes requeridos

  • python
  • todas las librerías utilizadas por la aplicación
  • py2exe
  • nsis
  • tiempo y suerte

orden de instalación

algunos instaladores son independientes de otros, pero para evitar posibles problemas recomiendo la instalación en el siguiente orden.

  • python
  • gtk-runtime
  • gtk2-themes
  • nsis
  • pygobject
  • pycairo
  • pygtk
  • pywin32
  • py2exe

tareas extra

  • setear la variable de entorno PATH para agregar el path a la instalación de python
  • probar la instalación con una pequeña aplicación gtk
>>> import gtk
>>> w = gtk.Window()
>>> l = gtk.Label("asd")
>>> w.add(l)
>>> w.show_all()
>>> gtk.main()

prueba con una aplicación de ejemplo

Cree un repositorio con una aplicación de ejemplo para probar los pasos, la aplicación esta disponible en github acá:

http://github.com/marianoguerra/PyGtkOnWindows

pasos

  • descargarla
  • descomprimirla
  • ejecutar python setup.py py2exe
  • copiar los directorios lib y share de la instalación del runtime de gtk (no de la instalación de pygtk) al directorio dist
  • copiar todos los archivos del directorio dll al directorio dist
  • borrar los locales y temas no usados de los directorios copiados a dist (yo solo dejo el theme MS-Windows)
  • crear la siguiente estructura de directorios dentro de dist: etc/gtk-2.0
  • dentro de ese directorio crear un archivo llamado gtkrc con una linea como la siguiente dentro:
    • gtk-theme-name = "MS-Windows"
    • podes cambiar el tema usado manteniendo otro theme dentro de share/themes y cambiando el nombre del theme en gtkrc
  • right click en ejemplo.nsi y seleccionar "Compile NSIS Script"
  • right click en ejemplo-portable.nsi y seleccionar "Compile NSIS Script"
  • deberías tener el instalador y la versión portable disponibles
  • para probar que funciona correctamente, correr el instalador y la versión portable en una instalación de windows sin los paquetes que instalaste anteriormente

probar con una aplicación real

ahora para sentirlo mas real, creemos un instalador y una versión portable de
un programa real, en este caso, un proyecto personal llamado emesene 2
(http://www.emesene.org/).

pasos

  • descargarlo de http://github.com/emesene/emesene
  • descomprimirlo
  • copiar setup.py and ez_setup.py al directorio emesene
  • cd emesene
  • correr python setup.py py2exe
  • cd ..
  • copiar los directorios lib y share de la instalación del runtime de gtk (no de la instalación de pygtk) al directorio dist
  • copiar todos los archivos del directorio dll al directorio dist
  • borrar los locales y temas no usados de los directorios copiados a dist (yo solo dejo el theme MS-Windows)
  • crear la siguiente estructura de directorios dentro de dist: etc/gtk-2.0
  • dentro de ese directorio crear un archivo llamado gtkrc con una linea como la siguiente dentro:
    • gtk-theme-name = "MS-Windows"
    • podes cambiar el tema usado manteniendo otro theme dentro de share/themes y cambiando el nombre del theme en gtkrc
  • right click en emesene.nsi y seleccionar "Compile NSIS Script"
  • right click en emesene-portable.nsi y seleccionar "Compile NSIS Script"
  • deberías tener el instalador y la versión portable disponibles
  • para probar que funciona correctamente, correr el instalador y la versión portable en una instalación de windows sin los paquetes que instalaste anteriormente

notas

[EN] How to generate .exe files and installers for a python (and pygtk) applications

How to generate .exe files and installers for a python applications


This document describes the steps required to create an executable file from a
python program and how to build an installer and portable file from that
application.


The document assumes that the application is based on GTK but it should work
with minor changes for other toolkits.

why an installer

  • many components are required to install by hand by the end user for a simple application
  • a lot of small installers
  • hard to find
  • hard to match the exact versions that work together
  • install them in the required order
  • pray
  • sometimes even doing everything right it may not work
  • easy to automate and document to replicate with each new version
  • free the end user from problems to use the app

required components

  • python
  • all the libraries used by the application
  • py2exe
  • nsis
  • time and luck ;)

installation order


some installers are independent from the others, but to avoid posible problems I recommend the installation in this order.

  • python
  • gtk-runtime
  • gtk2-themes
  • nsis
  • pygobject
  • pycairo
  • pygtk
  • pywin32
  • py2exe

extra tasks

  • set the PATH environment variable to add the path to the python installation
  • test that the installation works with a simple gtk application
>>> import gtk

>>> w = gtk.Window()
>>> l = gtk.Label("asd")
>>> w.add(l)
>>> w.show_all()
>>> gtk.main()

test with a sample application


I created a repository with a sample application to test the steps, the application is available in github here:

http://github.com/marianoguerra/PyGtkOnWindows

steps

  • download it
  • unpack it
  • run python setup.py py2exe
  • copy the lib and share directory from the gtk runtime installation (not the pygtk installation) to the dist directory
  • copy all the files from the dll directory to the dist directory
  • remove unused locales and unused themes (I keep only ms theme)
  • create the following dirs inside dist: etc/gtk-2.0
  • inside that create a file called gtkrc with a line like this inside:
    • gtk-theme-name = "MS-Windows"
    • you can change the theme by keeping that theme inside share/themes and changing the name in gtkrc
  • right click on ejemplo.nsi and select "Compile NSIS Script"
  • right click on ejemplo-portable.nsi and select "Compile NSIS Script"
  • you should have the installer and portable versions available
  • to test that it works correctly, run the installer and portable versions in a windows installation without the packages you installed previously

test with a real application

now to make it feel more real let's create an installer and portable versions
for a real world program, in this case, a project of mine called emesene 2
(http://www.emesene.org/).

steps


  • download it from http://github.com/emesene/emesene
  • unpack it
  • copy setup.py and ez_setup.py to the emesene directory
  • cd to emesene
  • run python setup.py py2exe
  • cd ..
  • copy the lib and share directory from the gtk runtime installation (not the pygtk installation) to the dist directory
  • copy all the files from the dll directory to the dist directory
  • remove unused locales and unused themes (I keep only ms theme)
  • create the following dirs inside dist: etc/gtk-2.0
  • inside that create a file called gtkrc with a line like this inside:
    • gtk-theme-name = "MS-Windows"
    • you can change the theme by keeping that theme inside share/themes and changing the name in gtkrc
  • right click on emesene.nsi and select "Compile NSIS Script"
  • right click on emesene-portable.nsi and select "Compile NSIS Script"
  • you should have the installer and portable versions available
  • to test that it works correctly, run the installer and portable versions in a windows installation without the packages you installed previously

notes

Seguidores

Archivo del Blog