|[ LiB ]|
You can execute .py files once Python is installed on your machine, but that doesn't make your Python game programs universally playable . You still need to convert your scripts into a bundled executable for whatever platform you want to run on. Luckily, there are a few resources for accomplishing just that.
When modules are imported in Python by other modules, Python compiles the relevant code into byte-code, an intermediate, portable, closer-to-low-level binary language form. This byte-code is stored with the .pyc suffix, short for Python compiled , instead of the typical .py.
Python's .pyc files correspond roughly to DLLs (dynamically loaded libraries) used in C. Regular .py modules can be used dynamically, too, but the compiled Python code is tighter and Python interprets the code at runtime when the file is imported.
Precompiling scripts is one way to speed up Python programs that need to import many modules. You can minimize a program's startup time by making sure source code is kept in directories where Python will have access to writing .pyc files.
You can also ship Python programs as .pyc files rather than as .py scripts. Since .pyc files are binary, they cannot be run as scripts, but they can be sent to the Python interpreter; simply add the name of the .pyc file the next time you run Python, like this:
In order to build a compiled Python file from the Python interpreter, import the compile function from py_compile and run the compile command, like this:
from py_compile import compile compile("script_to_compile.py")
Freeze is a system that takes Python script files and turns them into modules packaged into C files. Originally Freeze was used as one way to ship Python source, but it is now mostly defunct , although it will still be available in Version 2.3 for backwards -compatibility. The compiled script that Freeze generates allows a Python program to ship without the source code in plain view and without using .pyc files. The benefits to Freeze are that you can ship Python as two .c files and a makefile instead of as a .py, and you can make Python runable on platforms that do not have Python installed. The downside is that Freeze doesn't work well initially with Tkinter and other Windows GUIs.
ActiveState is a company that focuses on applied open source. It creates development packages for software developers and provides resources for Perl, Python, and PHP development. ActiveState currently has a Python distribution called ActivePython. It also supports creating Python RPM (Red Hat Package Managers) installers, Windows complete installers , and a Visual Studio .NET IDE plug-in for Python. These services (some are free, others not) are available at the ActiveState Python Website, at http://www.activestate.com/Solutions/Programmer/Python.plex.
The py2exe extension is an open source utility that converts Python scripts into executable Windows programs. The software is copyrighted by Thomas Heller but is freely distributable, and you'll find a copy with the license on the accompanying CD under Python/py2exe.
The extension is still under development but has expanded recently to include the ability to turn Python scripts into Windows NT-like services; it has been used to create a number of popular Python applications, such as wxPython, Tkinter, and pygame (you'll get to know these applications a bit better in the next chapter).
py2exe is a Distutils (Python Distribution Utilities) extension, and relies on the work by Greg Ward to make Python programs distributable (see the Disutils Website at http://www.python.org/doc/current/dist/). The Distutils are necessary for py2exe to work and are also included on the Python folder in this book's CD.
|[ LiB ]|