Distributing Your Program

This will be a short chapter because distributing your Python program to others is generally pretty easy. The main way for users to get your program is by getting the raw .py files and saving them to a storage location. Assuming the user has Python installed on his system, all he has to do is call your Python program like normal, e.g. python foo.py. This is the same method you have been using in this book and it is the easiest way to deal with Python files.

Another way of distributing your source code is providing the byte-code compiled versions; the files that end with .pyc that are created after you first run your program. These compiled versions are not the programmer-readable files of your program; they are the machine-ready files that are actually used by the computer. If you want a modicum of security with your program but don’t want the whole hassle of dealing with obfuscation or other ways of hiding your work, you may want to use .pyc files.

If you want to distribute your program like a “normal” Python application, i.e. invoking it with the setup.py command, you can use Python’s distutils module. I won’t go into specifics on this utility; suffice to say that many installable Python programs use this same method to package themselves. It helps to keep everything together and reduce the number of individual files a user needs to keep track of when running your program. More information about distutils can be found at http://docs.python.org/distutils/index.html.

Alternatively, you can package your program as a Python egg. Eggs are analogous to the JAR files in Java; they are simply a way of bundling information with a Python project, enabling run-time checking of program dependencies and allowing a program to provide plugins for other projects. A good example of a Python program that uses eggs is Django, the web framework project. The nice thing about Python eggs is that you can use the “Easy Install” package manager, which takes care of finding, downloading, and installing the egg files for you.

For people who don’t have (or want) Python installed on their systems, you can compile your program to a binary executable, e.g. a .exe file. For Unix and Linux systems, you can use the Freeze utility that is included with the Python language. Note that, for the most part, this is really unnecessary because Python is almost a default installed application on *nix operating systems and, generally speaking, people who are using these systems can probably handle working with Python files. But, the capability is there if you want to do it.

For Windows, which doesn’t have Python installed by default, you are more likely to want to create a standard executable file (though you can still offer the raw .py files for the more adventurous or power users). The utility for this is called py2exe. py2exe is a utility that converts Python scripts to normal executable Windows programs. Again, Python isn’t required to be installed on the computer so this is one of the most popular ways to create Python programs for Windows. However, the file size can be considerably larger for a converted program vs. the raw .py files.

Finally, for Mac users, you can use the py2app utility. It functions just like py2exe, creating a compiled version of your Python program for Mac users. OS X, the current operating system for Macs, includes Python with the OS install so technically you could just distribute the source code. But most Mac users don’t know how to use Python so creating a “normal” program for them is probably preferable.