Python 2.6 Graphics Cookbook
上QQ阅读APP看书,第一时间看更新

Make a compiled executable under Windows and Linux

How do we create and execute a.exe file that will run a compiled version of our Python and Tkinter programs? Can we make a self-contained folder that will run on an MS Windows or Linux distribution that uses a different version of Python from the ones we use? The answers to both questions are yes and the tool to achieve this is an Open Source program called cx_Freeze. Often what we would like to do is have our working Python program on a memory stick or downloadable on a network and be able to demonstrate it to friends, colleagues, or clients without the need to download Python onto the client's system. cx_Freeze allows us to create a distributable form of our Python graphic program.

Getting ready

You will need to download cx_Freeze from http://cx-freeze.sourceforge.net/. We need to pick a version that has the same version number as the Python version we are using. Currently, there are versions available from version 2.4 up to 3.1.

How to do it under MS Windows...

  1. MS Windows: Download cx_Freeze-4.2.win32-py2.6.msi, the windows installer for Python 2.6. If we have another Python version, then we must choose the appropriate installer from http://cx-freeze.sourceforge.net/.
  2. Save and run this installer.
  3. On completion of a successful Windows install we will see a folder named cx_Freeze inside \Python26\Lib\site-packages\.

How to do it under Linux (Debian and Ubuntu)...

  1. In a terminal run the command apt-get install cx-freeze.
  2. If this does not work we may need to first install a development-capable version of Python by running the command apt-get install python-dev. Then go back and repeat step 1.
  3. Test for success by typing in python in a command terminal to invoke the Python interpreter.
  4. Then after the >>> prompt, type import cx_Freeze. If the interpreter returns a new line and the >>> prompt again, without any complaints, we have been successful.

How to compile under both Linux and MS Windows...

  1. If the file we want to package as an executable is named walking_birdy_1.py in a folder called /constr, then we prepare a special setup file as follows.
    #setup.py
    from cx_Freeze import setup, Executable
    setup(executables=[Executable("/constr/walking_birdy_1.py")])
  2. Save it as setup.py.
  3. Then, in a command terminal run
    python /constr/setup.py build
  4. We will see a lot of system compilation commands scrolling down the command terminal that will eventually stop without error messages.
  5. We will find our complete self-contained executable inside a folder named build. Under Linux, we will find it inside our home directory under /build/exe.linux-i686-2.6. Under MS Windows, we will find it inside C:\Python26\build\exe.win-py2.6.
  6. We just need to copy the folder build with all its contents to wherever we want to run our self-contained program.

How it works...

A word of caution. If we use external files like images inside our code, then the path addresses of the files must be absolute because they are coded into, or frozen, into the executable version of our Python program. There are ways of setting up search paths which can be read at http://cx-freeze.sourceforge.net/cx_Freeze.html.

For example, say we want to use some GIF images in our program and then demonstrate them on other computers. First we place a folder called, for example, /birdy_pics, onto a USB memory stick. In the original program, walking_birdy_1.py, make sure the path addresses to the images point to the /birdy_pics folder on the stick. After compilation, copy the folder build onto the USB memory stick. Now when we double-click on the executable walking_birdy_1 it can locate the images on the USB memory stick when it needs to. These files include everything that is needed for your program, and you should distribute the whole directory contents to any user who wants to run your program without needing to install Python or Tkinter.

There is another program called py2exe that will also create executables to run on MS Windows. However, it cannot create self-contained binary executables to run under Linux whereas cx_Freeze can.