Portable Python distribution with PyGTK for Windows¶
Let’s say you developed a PyGTK application on a Linux platform and want now to port it to MS Windows. You’ll see that getting it to work under Windows is not straightforward. You need to install - at least - Python, PyGTK and the GTK+ runtime. You also have to change your PATH environment variable to let the system find the DLLs provided by the GTK runtime.
If you then need Pygtksourceview, you may have to manually replace some DLLs of your GTK runtime. Once you spent one afternoon doing that, your application runs, but you know that you cannot expect your users to repeat what you just did. You then have to find a better way to distribute your PyGTK program.
One option is Py2exe, a program which finds all the required DLLs and Python modules and puts them into one directory together with one executable which launches your application.
Unfortunately, Py2exe does not always work smoothly and sometimes it happens that you have to change the sources of your program so that it works once “compiled” with Py2exe. The subprocess module, for example, has a serious issue with Py2exe. This is really an inconvenient situation which may lead you to spend hours to get the Py2exe version to behave similarly to the Python one.
Moreover the Py2exe version is “closed source” and you may thus loose help from the Windows user who knows or is willing to learn how to use PyGTK, but won’t spend days installing stuff.
For sure, Py2exe can help to reduce the size of the final binary package. But you’ll have to distribute anyway the full GTK runtime, therefore the distributed package will be big anyway. The question is then: do you really need Py2exe?
What is provided inside the standalone PyGTK for Windows¶
The package that I put together can be used to distribute PyGTK applications OR simply as a developement environment for PyGTK. It indeed contains Py2exe, therefore you can install it and use Py2exe to “convert” your program to a EXE file (if you really need to do it). Typically, however, you would rather use the package in the following way:
- unzip it under the directory portable-pygtk;
- put the sources of your application under the directory portable-pygtk\pyapp;
- rewrite the Python source portable-pygtk\pyapp\main.py so that it launches your application (an example is provided later in the page);
- re-name the launcher portable-pygtk\launcher.exe and the directory portable-pygtk in the way you prefer (example myapp\myapp.exe) and re-zip the directory.
This way you get a portable version of your application for Windows. The user of your application should just unzip the archive and click on the file myapp\myapp.exe.
The file portable-pygtk\main.py¶
If your PyGTK application consists just of one file, then you can rename this file to portable-pygtk\main.py, removing the preexisting version (which launches a simple PyGTK Hello-World example). If your application is more complex, then you can put it under a directory like portable-pygtk\myappdir. You can change main.py so that it launches your application. One example is shown below:
# This is the file invoked by the launcher (the exe file in the top directory) import sys import os # Tell Python to search for modules under portable-pygtk\myappdir\src path_to_app = os.path.join(sys.path, "myappdir", "src") sys.path = path_to_app # Import the file portable-pygtk\myappdir\src\main.py import main # Launch the program by calling the function main() defined in main.py main.main(sys.argv)
This is just an example. main.py is parsed by the Python interpreter and you can write in the way it best suits your needs.